DROP TRIGGER LD0T0A01_UPD
GO
CREATE TRIGGER LD0T0A01_UPD ON LD0P0A
FOR UPDATE
AS
IF UPDATE(PNR) OR
   UPDATE(NAMN) OR
   UPDATE(ADRESS) OR
   UPDATE(COADR) OR
   UPDATE(POSTNR) OR
   UPDATE(ORT) OR
   UPDATE(HEMTEL) OR 
   UPDATE(ARBTEL) OR
   UPDATE(MOBIL) OR
   UPDATE(EPOSTHEM) OR
   UPDATE(ADRSKYDD) OR
   UPDATE(HEMLIGTFN) OR
   UPDATE(CIVKOD) OR
   UPDATE(CIVDAT) OR
   UPDATE(MEDBSKAP) OR
   UPDATE(MEDBDAT) OR
   UPDATE(FODFORS) OR
   UPDATE(FODLAND) OR
   UPDATE(OMRADE) OR
   UPDATE(FOLKFORS) OR
   UPDATE(FOLKLAN) OR
   UPDATE(FOLKKOM) OR
   UPDATE(FOLKDAT) OR
   UPDATE(AVREGORS) OR
   UPDATE(AVREGDAT) OR
   UPDATE(NYCKKOD) OR
   UPDATE(MODMAL) OR
   UPDATE(KOMSPRAK) OR
   UPDATE(KOMSPRAK2) OR 
   UPDATE(KOMSPRAK3) OR 
   UPDATE(INVANDAT) OR 
   UPDATE(TOLKBEHOV) OR 
   UPDATE(MINSIDA) OR
   UPDATE(KADRESS) OR
   UPDATE(KCOADR) OR
   UPDATE(KPOSTNR) OR
   UPDATE(KORT) OR
   UPDATE(KHEMTEL) OR
   UPDATE(KUATDAT) OR
   UPDATE(KFLYKTING) OR
   UPDATE(SMSAVIS) OR
   UPDATE(MAILAVIS) OR
   UPDATE(LCMEDDELANDE) OR
   UPDATE(MINSIDA_VUXNA)
 BEGIN
     DECLARE  
  @PNR          VARCHAR(12),
  @NAMN         VARCHAR(180),
  @ADRESS       VARCHAR(70),
  @COADR       VARCHAR(70),
  @POSTNR       VARCHAR(16),
  @ORT          VARCHAR(64),
  @HEMTEL      VARCHAR(32), 
  @ARBTEL      VARCHAR(32),
  @MOBIL    VARCHAR(32),
  @EPOSTHEM   VARCHAR(100),
  @ADRSKYDD     DECIMAL(1),
  @HEMLIGTFN   DECIMAL(1),
  @CIVKOD    DECIMAL(9),
  @CIVDAT       VARCHAR(8),
  @MEDBSKAP     DECIMAL(9),
  @MEDBDAT      VARCHAR(8),
  @FODFORS      DECIMAL(9),
  @FODLAND      DECIMAL(9),
  @OMRADE      DECIMAL(9),
  @FOLKFORS		DECIMAL(9),
  @FOLKLAN      DECIMAL(9),
  @FOLKKOM      DECIMAL(9),
  @FOLKDAT      VARCHAR(8),
  @AVREGORS     DECIMAL(9),
  @AVREGDAT     VARCHAR(8),
  @NYCKKOD      VARCHAR(24),
  @MODMAL       DECIMAL(9),
  @KOMSPRAK   DECIMAL(9),
  @KOMSPRAK2   DECIMAL(9),
  @KOMSPRAK3   DECIMAL(9),
  @INVANDAT   VARCHAR(8),
  @TOLKBEHOV   DECIMAL(1),
  @MINSIDA    DECIMAL(1),                     
  @KADRESS      VARCHAR(70), 
  @KCOADR      VARCHAR(70), 
  @KPOSTNR       VARCHAR(5),
  @KORT       VARCHAR(27),
  @KHEMTEL      VARCHAR(18),
  @KUATDAT      VARCHAR(8), 
  @KFLYKTING      DECIMAL(1),
  @SMSAVIS		DECIMAL(1),
  @MAILAVIS		DECIMAL(1),
  @LCMEDDELANDE DECIMAL(1),
  @MINSIDA_VUXNA DECIMAL(1),
  @UPDDAT       VARCHAR(8),
  @UPDSIGN      VARCHAR(10),
  
  @NEWPNR          VARCHAR(12),
  @NEWNAMN         VARCHAR(180),
  @NEWADRESS       VARCHAR(70),
  @NEWCOADR       VARCHAR(70),
  @NEWPOSTNR       VARCHAR(16),
  @NEWORT          VARCHAR(64),
  @NEWHEMTEL      VARCHAR(32), 
  @NEWARBTEL      VARCHAR(32),
  @NEWMOBIL    VARCHAR(32),
  @NEWEPOSTHEM   VARCHAR(100),
  @NEWADRSKYDD     DECIMAL(1),
  @NEWHEMLIGTFN   DECIMAL(1),
  @NEWCIVKOD    DECIMAL(9),
  @NEWCIVDAT       VARCHAR(8),
  @NEWMEDBSKAP     DECIMAL(9),
  @NEWMEDBDAT      VARCHAR(8),
  @NEWFODFORS      DECIMAL(9),
  @NEWFODLAND      DECIMAL(9),
  @NEWOMRADE      DECIMAL(9),
  @NEWFOLKFORS		DECIMAL(9),
  @NEWFOLKLAN      DECIMAL(9),
  @NEWFOLKKOM      DECIMAL(9),
  @NEWFOLKDAT      VARCHAR(8),
  @NEWAVREGORS     DECIMAL(9),
  @NEWAVREGDAT     VARCHAR(8),
  @NEWNYCKKOD      VARCHAR(24),
  @NEWMODMAL       DECIMAL(9),
  @NEWKOMSPRAK   DECIMAL(9),
  @NEWKOMSPRAK2   DECIMAL(9),
  @NEWKOMSPRAK3   DECIMAL(9),
  @NEWINVANDAT   VARCHAR(8),
  @NEWTOLKBEHOV   DECIMAL(1),
  @NEWMINSIDA    DECIMAL(1),                     
  @NEWKADRESS      VARCHAR(70), 
  @NEWKCOADR      VARCHAR(70), 
  @NEWKPOSTNR       VARCHAR(5),
  @NEWKORT       VARCHAR(27),
  @NEWKHEMTEL      VARCHAR(18),
  @NEWKUATDAT      VARCHAR(8), 
  @NEWKFLYKTING      DECIMAL(1),
  @NEWSMSAVIS     DECIMAL(1),
  @NEWMAILAVIS      DECIMAL(1),
  @NEWLCMEDDELANDE DECIMAL(1),
  @NEWMINSIDA_VUXNA DECIMAL(1),

  @NEWUPDDAT       VARCHAR(8),
  @NEWUPDSIGN      VARCHAR(10),

  @ANTALRADER    DECIMAL(9)

     SET @ANTALRADER = (SELECT COUNT(*) FROM INSERTED)
     
     DECLARE CURR_UPDPERSON CURSOR FOR 
     SELECT  PNR  ,  NAMN,  ADRESS,  COADR ,  POSTNR,  ORT,  HEMTEL,   ARBTEL,MOBIL,EPOSTHEM,  ADRSKYDD,  HEMLIGTFN
			,  CIVKOD,  CIVDAT,  MEDBSKAP,  MEDBDAT,FODFORS,  FODLAND,  OMRADE,FOLKFORS,FOLKLAN,FOLKKOM,FOLKDAT,AVREGORS,
			AVREGDAT,  NYCKKOD,MODMAL,  KOMSPRAK,  KOMSPRAK2,  KOMSPRAK3,  INVANDAT,  
		  TOLKBEHOV,  MINSIDA  ,  KADRESS,   KCOADR,   KPOSTNR,  KORT,
		  KHEMTEL,  KUATDAT,   KFLYKTING, UPDDAT,UPDSIGN,SMSAVIS,MAILAVIS,LCMEDDELANDE,MINSIDA_VUXNA  FROM DELETED

     OPEN CURR_UPDPERSON
     FETCH NEXT FROM CURR_UPDPERSON INTO  @PNR  ,  @NAMN,  @ADRESS,  @COADR ,  @POSTNR,  @ORT,  @HEMTEL, @ARBTEL,@MOBIL,@EPOSTHEM,  
			@ADRSKYDD,  @HEMLIGTFN	,  @CIVKOD,  @CIVDAT,  @MEDBSKAP,  @MEDBDAT,@FODFORS , @FODLAND,  @OMRADE,@FOLKFORS,@FOLKLAN,
			@FOLKKOM,@FOLKDAT,@AVREGORS,	@AVREGDAT,  @NYCKKOD,@MODMAL,  @KOMSPRAK,  @KOMSPRAK2,  @KOMSPRAK3,  @INVANDAT,  
		  @TOLKBEHOV,  @MINSIDA ,   @KADRESS,   @KCOADR,   @KPOSTNR,  @KORT,
		  @KHEMTEL,  @KUATDAT,   @KFLYKTING,@UPDDAT,@UPDSIGN,@SMSAVIS,@MAILAVIS,@LCMEDDELANDE,@MINSIDA_VUXNA

    WHILE @@FETCH_STATUS = 0
	BEGIN 
     
DECLARE
  @TABLE_OWNER VARCHAR(100)=(SELECT 
			SCH.NAME 
            FROM SYS.SCHEMAS SCH
            JOIN SYS.OBJECTS OBJ
            ON OBJ.schema_id=SCH.schema_id
            WHERE OBJ.NAME='LD0V0A'),
	@SQL NVARCHAR(4000)
SET @SQL = 'EXEC ' +@TABLE_OWNER +'.LD0_A01_INSERT @PNR,@NAMN,  @ADRESS,  @COADR ,  @POSTNR,  @ORT,  @HEMTEL,@ARBTEL,@MOBIL,@EPOSTHEM,  @ADRSKYDD,  @HEMLIGTFN
			,  @CIVKOD,  @CIVDAT,  @MEDBSKAP,  @MEDBDAT,@FODFORS , @FODLAND,  @OMRADE,@FOLKFORS,@FOLKLAN,@FOLKKOM,@FOLKDAT,@AVREGORS,
			@AVREGDAT,  @NYCKKOD,@MODMAL,  @KOMSPRAK,  @KOMSPRAK2,  @KOMSPRAK3,  @INVANDAT,  
		  @TOLKBEHOV,  @MINSIDA  ,  @KADRESS,   @KCOADR,   @KPOSTNR,  @KORT,
		  @KHEMTEL,  @KUATDAT,   @KFLYKTING,@SMSAVIS,@MAILAVIS,@LCMEDDELANDE, @MINSIDA_VUXNA,@UPDDAT,@UPDSIGN'
EXECUTE 
SP_EXECUTESQL @SQL
,N'@PNR  VARCHAR(12),  @NAMN VARCHAR(180),@ADRESS       VARCHAR(70),  @COADR       VARCHAR(70),  @POSTNR       VARCHAR(16),  @ORT          VARCHAR(64),  @HEMTEL      VARCHAR(32),   @ARBTEL      VARCHAR(32),
  @MOBIL    VARCHAR(32),  @EPOSTHEM   VARCHAR(100),  @ADRSKYDD     DECIMAL(1),  @HEMLIGTFN   DECIMAL(1),  @CIVKOD    DECIMAL(9),  @CIVDAT       VARCHAR(8),  @MEDBSKAP     DECIMAL(9),
    @MEDBDAT      VARCHAR(8),  @FODFORS      DECIMAL(9),  @FODLAND      DECIMAL(9),  @OMRADE      DECIMAL(9),  @FOLKFORS		DECIMAL(9),  @FOLKLAN      DECIMAL(9),
	  @FOLKKOM      DECIMAL(9),  @FOLKDAT      VARCHAR(8),  @AVREGORS     DECIMAL(9),  @AVREGDAT     VARCHAR(8),  @NYCKKOD      VARCHAR(24),  @MODMAL       DECIMAL(9),
	    @KOMSPRAK   DECIMAL(9),  @KOMSPRAK2   DECIMAL(9),  @KOMSPRAK3   DECIMAL(9),  @INVANDAT   VARCHAR(8),  @TOLKBEHOV   DECIMAL(1),  @MINSIDA    DECIMAL(1),   @KADRESS      VARCHAR(70),
		  @KCOADR      VARCHAR(70),   @KPOSTNR       VARCHAR(5),  @KORT       VARCHAR(27),  @KHEMTEL      VARCHAR(18),  @KUATDAT      VARCHAR(8),   @KFLYKTING      DECIMAL(1),
		    @SMSAVIS		DECIMAL(1),  @MAILAVIS		DECIMAL(1),  @LCMEDDELANDE DECIMAL(1),  @MINSIDA_VUXNA DECIMAL(1),  @UPDDAT       VARCHAR(8),  @UPDSIGN      VARCHAR(10)'
,@PNR=@PNR,@NAMN=@NAMN, @ADRESS=@ADRESS,@COADR=@COADR,@POSTNR=@POSTNR,@ORT=@ORT,@HEMTEL=@HEMTEL,@ARBTEL=@ARBTEL,@MOBIL=@MOBIL,@EPOSTHEM=@EPOSTHEM,@ADRSKYDD=@ADRSKYDD,@HEMLIGTFN=@HEMLIGTFN,
@CIVKOD=@CIVKOD,@CIVDAT=@CIVDAT,@MEDBSKAP=@MEDBSKAP,@MEDBDAT=@MEDBDAT,@FODFORS=@FODFORS,@FODLAND=@FODLAND,@OMRADE=@OMRADE,@FOLKFORS=@FOLKFORS,@FOLKLAN=@FOLKLAN,
@FOLKKOM=@FOLKKOM,@FOLKDAT=@FOLKDAT,@AVREGORS=@AVREGORS,@AVREGDAT=@AVREGDAT,@NYCKKOD=@NYCKKOD,@MODMAL=@MODMAL,@KOMSPRAK=@KOMSPRAK,@KOMSPRAK2=@KOMSPRAK2,@KOMSPRAK3=@KOMSPRAK3,
@INVANDAT=@INVANDAT,@TOLKBEHOV=@TOLKBEHOV,@MINSIDA=@MINSIDA,@KADRESS=@KADRESS,@KCOADR=@KCOADR,@KPOSTNR=@KPOSTNR,@KORT=@KORT,@KHEMTEL=@KHEMTEL,@KUATDAT=@KUATDAT,
@KFLYKTING=@KFLYKTING,@SMSAVIS=@SMSAVIS,@MAILAVIS=@MAILAVIS,@LCMEDDELANDE=@LCMEDDELANDE,@MINSIDA_VUXNA=@MINSIDA_VUXNA,@UPDDAT=@UPDDAT,@UPDSIGN=@UPDSIGN

     IF (@ANTALRADER = 1)
       SELECT	  @NEWPNR  = PNR,
				  @NEWNAMN = NAMN,
				  @NEWADRESS = ADRESS,
				  @NEWCOADR = COADR,
				  @NEWPOSTNR=POSTNR,
				  @NEWORT=ORT,
				  @NEWHEMTEL=HEMTEL, 
				  @NEWARBTEL=ARBTEL, 
				  @NEWKADRESS=KADRESS, 
				  @NEWKCOADR=KCOADR, 
				  @NEWKPOSTNR=KPOSTNR,
				  @NEWKORT=KORT,
				  @NEWKHEMTEL=KHEMTEL,
				  @NEWKUATDAT=KUATDAT, 
				  @NEWKFLYKTING=KFLYKTING,
				  @NEWCIVKOD=CIVKOD,
				  @NEWCIVDAT=CIVDAT,
				  @NEWMEDBSKAP=MEDBSKAP,
				  @NEWMEDBDAT=MEDBDAT,
				  @NEWFODFORS=FODFORS,
				  @NEWFODLAND=FODLAND,
				  @NEWOMRADE=OMRADE,
				  @NEWFOLKLAN=FOLKLAN,
				  @NEWFOLKKOM=FOLKKOM,
				  @NEWFOLKDAT=FOLKDAT,
				  @NEWAVREGORS=AVREGORS,
				  @NEWAVREGDAT=AVREGDAT,
				  @NEWADRSKYDD=ADRSKYDD,
				  @NEWHEMLIGTFN=HEMLIGTFN,
				  @NEWNYCKKOD=NYCKKOD,
				  @NEWMOBIL=MOBIL,
				  @NEWEPOSTHEM=EPOSTHEM,
				  @NEWMODMAL=MODMAL,
				  @NEWKOMSPRAK=KOMSPRAK,
				  @NEWKOMSPRAK2=KOMSPRAK2,
				  @NEWKOMSPRAK3=KOMSPRAK3,
				  @NEWINVANDAT=INVANDAT,
				  @NEWTOLKBEHOV=TOLKBEHOV,
				  @NEWMINSIDA=MINSIDA ,
				  @NEWSMSAVIS=SMSAVIS,
				  @NEWMAILAVIS=MAILAVIS,
				  @NEWLCMEDDELANDE=LCMEDDELANDE,
				  @NEWMINSIDA_VUXNA=MINSIDA_VUXNA,
				  @NEWUPDDAT=UPDDAT,
				  @NEWUPDSIGN=UPDSIGN
					FROM INSERTED
     ELSE
       SELECT	  @NEWPNR  = PNR,
				  @NEWNAMN = NAMN,
				  @NEWADRESS = ADRESS,
				  @NEWCOADR = COADR,
				  @NEWPOSTNR=POSTNR,
				  @NEWORT=ORT,
				  @NEWHEMTEL=HEMTEL, 
				  @NEWARBTEL=ARBTEL, 
				  @NEWKADRESS=KADRESS, 
				  @NEWKCOADR=KCOADR, 
				  @NEWKPOSTNR=KPOSTNR,
				  @NEWKORT=KORT,
				  @NEWKHEMTEL=KHEMTEL,
				  @NEWKUATDAT=KUATDAT, 
				  @NEWKFLYKTING=KFLYKTING,
				  @NEWCIVKOD=CIVKOD,
				  @NEWCIVDAT=CIVDAT,
				  @NEWMEDBSKAP=MEDBSKAP,
				  @NEWMEDBDAT=MEDBDAT,
				  @NEWFODFORS=FODFORS,
				  @NEWFODLAND=FODLAND,
				  @NEWOMRADE=OMRADE,
				  @NEWFOLKLAN=FOLKLAN,
				  @NEWFOLKKOM=FOLKKOM,
				  @NEWFOLKDAT=FOLKDAT,
				  @NEWAVREGORS=AVREGORS,
				  @NEWAVREGDAT=AVREGDAT,
				  @NEWADRSKYDD=ADRSKYDD,
				  @NEWHEMLIGTFN=HEMLIGTFN,
				  @NEWNYCKKOD=NYCKKOD,
				  @NEWMOBIL=MOBIL,
				  @NEWEPOSTHEM=EPOSTHEM,
				  @NEWMODMAL=MODMAL,
				  @NEWKOMSPRAK=KOMSPRAK,
				  @NEWKOMSPRAK2=KOMSPRAK2,
				  @NEWKOMSPRAK3=KOMSPRAK3,
				  @NEWINVANDAT=INVANDAT,
				  @NEWTOLKBEHOV=TOLKBEHOV,
				  @NEWMINSIDA=MINSIDA ,
				  @NEWSMSAVIS=SMSAVIS,
				  @NEWMAILAVIS=MAILAVIS,
				  @NEWLCMEDDELANDE=LCMEDDELANDE,
				  @NEWMINSIDA_VUXNA=MINSIDA_VUXNA,
				  @NEWUPDDAT=UPDDAT,
				  @NEWUPDSIGN=UPDSIGN
					FROM INSERTED WHERE	PNR = @PNR

IF (@PNR<>@NEWPNR) 
BEGIN
 IF ((SELECT COUNT(*) FROM LD0P0A01 WHERE PNR = @NEWPNR) > 0)
   DELETE FROM LD0P0A01 WHERE PNR = @NEWPNR

 UPDATE LD0P0A01 SET
 PNR = @NEWPNR
 WHERE PNR=@PNR 

 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWPNR,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Personnr' AND 
 PNR = @NEWPNR
END

IF (@NAMN<>@NEWNAMN) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWNAMN,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Namn' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@ADRESS<>@NEWADRESS) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWADRESS,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Adress' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@COADR<>@NEWCOADR) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWCOADR,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='C/O Adress' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@POSTNR<>@NEWPOSTNR) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWPOSTNR,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Postnr' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@ORT<>@NEWORT) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWORT,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Ort' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@HEMTEL<>@NEWHEMTEL AND @NEWHEMTEL<>' ') 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWHEMTEL,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Hemtelefon' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@ARBTEL<>@NEWARBTEL AND @NEWARBTEL<>' ') 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWARBTEL,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Arbetstelefon' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@MOBIL<>@NEWMOBIL AND @NEWMOBIL<>' ')
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWMOBIL,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Mobil' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@EPOSTHEM<>@NEWEPOSTHEM AND @NEWEPOSTHEM<>' ')
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWEPOSTHEM,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='E-post hem' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@ADRSKYDD<>@NEWADRSKYDD) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWADRSKYDD,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Adresskydd' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@HEMLIGTFN<>@NEWHEMLIGTFN) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWHEMLIGTFN,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Hemligt telefonnummer' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@CIVKOD<>@NEWCIVKOD) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWCIVKOD,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Civilståndskod' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@CIVDAT<>@NEWCIVDAT) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWCIVDAT,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Datum för Civilståndsändring' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@MEDBSKAP<>@NEWMEDBSKAP) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWMEDBSKAP,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Medborgarskap' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@MEDBDAT<>@NEWMEDBDAT) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWMEDBDAT,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Medborgarskap datum' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@FODFORS<>@NEWFODFORS) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWFODFORS,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Födelseförsamling' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@FODLAND<>@NEWFODLAND) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWFODLAND,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Födelseland' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@OMRADE<>@NEWOMRADE) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWOMRADE,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Område' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@FOLKFORS<>@NEWFOLKFORS) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWFOLKFORS,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Församlingstillhörighet' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@FOLKLAN<>@NEWFOLKLAN) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWFOLKLAN,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Län' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@FOLKKOM<>@NEWFOLKKOM) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWFOLKKOM,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Hemkommun' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@FOLKDAT<>@NEWFOLKDAT) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWFOLKDAT,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Folkbokföringsdatum' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@AVREGORS<>@NEWAVREGORS) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWAVREGORS,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Avregistreringsorsak' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@AVREGDAT<>@NEWAVREGDAT) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWAVREGDAT,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Avregistreringsdatum' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@NYCKKOD<>@NEWNYCKKOD) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWNYCKKOD,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Nyckelkod' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@MODMAL<>@NEWMODMAL) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWMODMAL,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Modersmål' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@KOMSPRAK<>@NEWKOMSPRAK) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWKOMSPRAK,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Kommunikationsspråk' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@KOMSPRAK2<>@NEWKOMSPRAK2) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWKOMSPRAK2,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Kommunikationsspråk 2' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@KOMSPRAK3<>@NEWKOMSPRAK3) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWKOMSPRAK3,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Kommunikationsspråk 3' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@INVANDAT<>@NEWINVANDAT) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWINVANDAT,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Invandringsdatum' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@TOLKBEHOV<>@NEWTOLKBEHOV) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWTOLKBEHOV,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Tolkbehov' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@MINSIDA<>@NEWMINSIDA) 
 UPDATE LD0P0A01 SET
 NEWVALUE=CASE WHEN @NEWMINSIDA=1 THEN 'Ja' ELSE 'Nej' END,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Min sida Ek.bistånd' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@KADRESS<>@NEWKADRESS) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWKADRESS,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Klientens Adress' AND 
 LD0P0A01.PNR = @NEWPNR
 
IF (@KCOADR<>@NEWKCOADR) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWKCOADR,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Klientens C/O Adress' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@KPOSTNR<>@NEWKPOSTNR) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWKPOSTNR,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Klientens Postnr' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@KORT<>@NEWKORT) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWKORT,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Klientens Ort' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@KHEMTEL<>@NEWKHEMTEL) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWKHEMTEL,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Klientens Hemtelefon' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@KUATDAT<>@NEWKUATDAT) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWKUATDAT,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Klientens Datum för Uppehålls- och arbetstillstånd' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@KFLYKTING<>@NEWKFLYKTING) 
 UPDATE LD0P0A01 SET
 NEWVALUE=@NEWKFLYKTING,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Klienten Flykting' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@SMSAVIS<>@NEWSMSAVIS) 
 UPDATE LD0P0A01 SET
 NEWVALUE=CASE WHEN @NEWSMSAVIS=1 THEN 'Ja' ELSE 'Nej' END,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='SMS Notifiering' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@MAILAVIS<>@NEWMAILAVIS) 
 UPDATE LD0P0A01 SET
 NEWVALUE=CASE WHEN @NEWMAILAVIS=1 THEN 'Ja' ELSE 'Nej' END,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Epost Notifiering' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@LCMEDDELANDE<>@NEWLCMEDDELANDE) 
 UPDATE LD0P0A01 SET
 NEWVALUE=CASE WHEN @NEWLCMEDDELANDE=1 THEN 'Ja' ELSE 'Nej' END,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Meddelande' AND 
 LD0P0A01.PNR = @NEWPNR

IF (@MINSIDA_VUXNA<>@NEWMINSIDA_VUXNA) 
 UPDATE LD0P0A01 SET
 NEWVALUE=CASE WHEN @NEWMINSIDA_VUXNA=1 THEN 'Ja' ELSE 'Nej' END,
 UPDSIGN=@NEWUPDSIGN,
 UPDDAT=@NEWUPDDAT,
 DATASOURCE = ' '
 WHERE COLNAME='Min Sida Vuxen' AND 
 LD0P0A01.PNR = @NEWPNR

FETCH NEXT FROM CURR_UPDPERSON INTO  @PNR  ,  @NAMN,  @ADRESS,  @COADR ,  @POSTNR,  @ORT,  @HEMTEL, @ARBTEL,@MOBIL,@EPOSTHEM,  
			@ADRSKYDD,  @HEMLIGTFN	,  @CIVKOD,  @CIVDAT,  @MEDBSKAP,  @MEDBDAT,@FODFORS , @FODLAND,  @OMRADE,@FOLKFORS,@FOLKLAN,
			@FOLKKOM,@FOLKDAT,@AVREGORS,	@AVREGDAT,  @NYCKKOD,@MODMAL,  @KOMSPRAK,  @KOMSPRAK2,  @KOMSPRAK3,  @INVANDAT,  
		  @TOLKBEHOV,  @MINSIDA ,   @KADRESS,   @KCOADR,   @KPOSTNR,  @KORT,
		  @KHEMTEL,  @KUATDAT,   @KFLYKTING, @UPDDAT,@UPDSIGN,@SMSAVIS,@MAILAVIS,@LCMEDDELANDE,@MINSIDA_VUXNA
END
CLOSE CURR_UPDPERSON

DEALLOCATE CURR_UPDPERSON

END
GO
GRANT ALL ON LD0_A01_INSERT TO LD0ROANV
GO