DROP PROCEDURE LD0_CHANGE_PNR 
;
CREATE PROCEDURE LD0_CHANGE_PNR 
@VC_OLDPNR VARCHAR(12),
@VC_NEWPNR VARCHAR(12), 
@VC_FODDAT VARCHAR(8),
@S_KON INT /*0=FEMALE,1=MALE*/,
@VC_UPDSIGN VARCHAR(10),
@VC_UPDDAT VARCHAR(8)
AS
BEGIN
  UPDATE LD0P0202 SET XINFO1=@VC_NEWPNR WHERE XINFO1=@VC_OLDPNR
  UPDATE LD0P0A SET PNR = @VC_NEWPNR , FODDAT = @VC_FODDAT, 
  KON = @S_KON, UPDSIGN = @VC_UPDSIGN, UPDDAT = @VC_UPDDAT WHERE PNR = @VC_OLDPNR 
  UPDATE LD0P0A_KQ0P0PER SET PNR = @VC_NEWPNR , FODDAT = @VC_FODDAT, 
  KON = @S_KON, UPDSIGN = @VC_UPDSIGN, UPDDAT = @VC_UPDDAT WHERE PNR = @VC_OLDPNR 
  UPDATE LD0P0B   SET AKTPNR    = @VC_NEWPNR WHERE AKTPNR    = @VC_OLDPNR
  UPDATE LD0P0C   SET AKTPNR    = @VC_NEWPNR WHERE AKTPNR    = @VC_OLDPNR
  UPDATE LD0P0C10 SET AKTPNR    = @VC_NEWPNR WHERE AKTPNR    = @VC_OLDPNR
  UPDATE LD0P0C20 SET AKTPNR    = @VC_NEWPNR WHERE AKTPNR    = @VC_OLDPNR
  UPDATE LD0P0C40 SET AKTPNR    = @VC_NEWPNR WHERE AKTPNR    = @VC_OLDPNR
  UPDATE LD0P0C45 SET BARNPNR    = @VC_NEWPNR WHERE BARNPNR    = @VC_OLDPNR
  UPDATE LD0P0C45 SET MODERPNR    = @VC_NEWPNR WHERE MODERPNR   = @VC_OLDPNR
  UPDATE LD0P0E14 SET AKTPNR    = @VC_NEWPNR WHERE AKTPNR    = @VC_OLDPNR
  UPDATE LD0P0R02 SET BOBPNR1   = @VC_NEWPNR WHERE BOBPNR1   = @VC_OLDPNR
  UPDATE LD0P0R02 SET BOBPNR2   = @VC_NEWPNR WHERE BOBPNR2   = @VC_OLDPNR
  UPDATE LD0P0X30 SET DOSSIELED = @VC_NEWPNR WHERE DOSSIELED = @VC_OLDPNR
  UPDATE LD0P0P   SET ID        = @VC_NEWPNR WHERE ID        = @VC_OLDPNR
  UPDATE LD0P0T   SET ID        = @VC_NEWPNR WHERE ID        = @VC_OLDPNR
  UPDATE LD0P0L50 SET LONPNR    = @VC_NEWPNR WHERE LONPNR    = @VC_OLDPNR
  UPDATE LD0P0LN0 SET NATPNR    = @VC_NEWPNR WHERE NATPNR    = @VC_OLDPNR
  UPDATE LD0P0U43 SET MEDSOKPNR = @VC_NEWPNR WHERE MEDSOKPNR = @VC_OLDPNR
  UPDATE LD0P0U43 SET TLANMEDPNR = @VC_NEWPNR WHERE TLANMEDPNR = @VC_OLDPNR
  UPDATE LD0P0H   SET MODER     = @VC_NEWPNR WHERE MODER     = @VC_OLDPNR
  UPDATE LD0P0H10 SET MEDSOKPNR = @VC_NEWPNR WHERE MEDSOKPNR = @VC_OLDPNR
  UPDATE LD0P0LH1 SET PERSONNR  = @VC_NEWPNR WHERE PERSONNR  = @VC_OLDPNR 
  UPDATE LD0P0LH6 SET PERSONNR  = @VC_NEWPNR WHERE PERSONNR  = @VC_OLDPNR
  UPDATE LD0P0LH9 SET PERSONNR  = @VC_NEWPNR WHERE PERSONNR  = @VC_OLDPNR
  UPDATE LD0P0LHC SET PERSONNR  = @VC_NEWPNR WHERE PERSONNR  = @VC_OLDPNR
  UPDATE LD0P0T41 SET PERSONNR  = @VC_NEWPNR WHERE PERSONNR  = @VC_OLDPNR
  UPDATE LD0P0U05 SET PERSONNR  = @VC_NEWPNR WHERE PERSONNR  = @VC_OLDPNR
  UPDATE LD0P0U01 SET PERSONNR  = @VC_NEWPNR WHERE PERSONNR  = @VC_OLDPNR
  UPDATE LD0P0U10 SET PERSONNR  = @VC_NEWPNR WHERE PERSONNR  = @VC_OLDPNR
  UPDATE LD0P0201 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0A20 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0A30 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0A31 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0A40 SET PERSONNR  = @VC_NEWPNR WHERE PERSONNR  = @VC_OLDPNR
  UPDATE LD0P0A45 SET PERSONNR  = @VC_NEWPNR WHERE PERSONNR  = @VC_OLDPNR
  UPDATE LD0P0A60 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0A70 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0A700 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0B10 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0B12 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0B13 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0B20 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0B22 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0B23 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0B24 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0B25 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0BSP SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0BVK SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0BVK SET OBJEKTNR  = @VC_NEWPNR WHERE OBJEKTNR  = @VC_OLDPNR
  UPDATE LD0P0BVK SET HUVUDOBJNR  = @VC_NEWPNR WHERE HUVUDOBJNR  = @VC_OLDPNR
  UPDATE LD0P0C01 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0C20 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0C21 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0C23 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0C31 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0C45 SET BARNPNR   = @VC_NEWPNR WHERE BARNPNR   = @VC_OLDPNR
  UPDATE LD0P0C45 SET MODERPNR   = @VC_NEWPNR WHERE MODERPNR = @VC_OLDPNR
  UPDATE LD0P0C71 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0D01 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0D20 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0D50 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR  
  UPDATE LD0P0E01 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0E03 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0E07 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0E13 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0E20 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0E30 SET PNR		= @VC_NEWPNR WHERE PNR		 = @VC_OLDPNR
  UPDATE LD0P0E40 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0E41 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0E42 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0E44 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0F01 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0F26 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0F30 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0F30 SET BETMOT_PNR = @VC_NEWPNR WHERE BETMOT_PNR = @VC_OLDPNR
  UPDATE LD0P0F31 SET BETMOTTPNR = @VC_NEWPNR WHERE BETMOTTPNR = @VC_OLDPNR
  UPDATE LD0P0F40 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0G01 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0H   SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0H40 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0K01 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0K02 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0L   SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0L03 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR  
  UPDATE LD0P0L20 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0L50 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0L80 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0L81 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0L83 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0L90 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0L93 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0L94 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0L95 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0L96 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0L98 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0L99 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0LN0 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0MLO SET MOTTAGPNR = @VC_NEWPNR WHERE MOTTAGPNR       = @VC_OLDPNR
  UPDATE LD0P0MLO SET AKTPNR    = @VC_NEWPNR WHERE AKTPNR       = @VC_OLDPNR
  UPDATE LD0P0M10 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0M20 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0N   SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0N20 SET DEBPNR       = @VC_NEWPNR WHERE DEBPNR       = @VC_OLDPNR
  UPDATE LD0P0N81 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0N90 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0R50 SET GODMANPNR = @VC_NEWPNR WHERE GODMANPNR = @VC_OLDPNR
  UPDATE LD0P0R50 SET VARDHAV1PNR = @VC_NEWPNR WHERE VARDHAV1PNR = @VC_OLDPNR
  UPDATE LD0P0R50 SET VARDHAV2PNR = @VC_NEWPNR WHERE VARDHAV2PNR = @VC_OLDPNR  
  UPDATE LD0P0R50 SET SFORVARDHAVPNR = @VC_NEWPNR WHERE SFORVARDHAVPNR = @VC_OLDPNR  
  UPDATE LD0P0R50 SET SFORVARDHAV2PNR = @VC_NEWPNR WHERE SFORVARDHAV2PNR = @VC_OLDPNR
  UPDATE LD0P0S05 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0S18 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0SEKR SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0SOCIALWELFARECUSTSHIP SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0T60 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0THLTEMP SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0V50 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0Y   SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0Y10 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0X40   SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0X41   SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0C03 SET REFPNR    = @VC_NEWPNR WHERE REFPNR    = @VC_OLDPNR
  UPDATE LD0P0E05 SET REFPNR    = @VC_NEWPNR WHERE REFPNR    = @VC_OLDPNR
  UPDATE LD0P0Y   SET REFPNR    = @VC_NEWPNR WHERE REFPNR    = @VC_OLDPNR
  UPDATE LD0P0Y11 SET RELPNR    = @VC_NEWPNR WHERE RELPNR    = @VC_OLDPNR
  UPDATE LD0P0Y15 SET PNR    = @VC_NEWPNR WHERE PNR    = @VC_OLDPNR
  UPDATE LD0P0Y15 SET PNR_VHAVARE    = @VC_NEWPNR WHERE PNR_VHAVARE    = @VC_OLDPNR
  UPDATE LD0P0Z50 SET PNR    = @VC_NEWPNR WHERE PNR    = @VC_OLDPNR
  UPDATE LD0P0R   SET RFVPNR    = @VC_NEWPNR WHERE RFVPNR    = @VC_OLDPNR
  UPDATE LD0P0R01 SET RFVPNR    = @VC_NEWPNR WHERE RFVPNR    = @VC_OLDPNR
  UPDATE LD0P0R02 SET RFVPNR    = @VC_NEWPNR WHERE RFVPNR    = @VC_OLDPNR
  UPDATE LD0P0R03 SET RFVPNR    = @VC_NEWPNR WHERE RFVPNR    = @VC_OLDPNR
  UPDATE LD0P0R04 SET RFVPNR    = @VC_NEWPNR WHERE RFVPNR    = @VC_OLDPNR
  UPDATE LD0P0R05 SET RFVPNR    = @VC_NEWPNR WHERE RFVPNR    = @VC_OLDPNR
  UPDATE LD0P0R11 SET RFVPNR    = @VC_NEWPNR WHERE RFVPNR    = @VC_OLDPNR
  UPDATE LD0P0R20 SET RFVPNR    = @VC_NEWPNR WHERE RFVPNR    = @VC_OLDPNR
  UPDATE LD0P0T44 SET PNRFROM   = @VC_NEWPNR WHERE PNRFROM   = @VC_OLDPNR
  UPDATE LD0P0T44 SET PNRTO     = @VC_NEWPNR WHERE PNRTO   = @VC_OLDPNR
  UPDATE LD0P0U83 SET PERSONNR  = @VC_NEWPNR WHERE PERSONNR  = @VC_OLDPNR
  UPDATE LD0P0U90 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0V10 SET OBJEKTNYKLALFA = @VC_NEWPNR WHERE OBJEKTNYKLALFA = @VC_OLDPNR
  UPDATE LD0P0V09 SET OBJEKTNYCKLALFA = @VC_NEWPNR WHERE OBJEKTNYCKLALFA = @VC_OLDPNR
  UPDATE LD0P0V18 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0P0V19 SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0PAA0INCOMPAPP SET PNR       = @VC_NEWPNR WHERE PNR       = @VC_OLDPNR
  UPDATE LD0PAA0INCOMPAPP SET MEDPNR       = @VC_NEWPNR WHERE MEDPNR       = @VC_OLDPNR
  UPDATE GA1P0TXT SET OBJEKTID = SUBSTRING(OBJEKTID, 1, LEN(OBJEKTID)-11) + @VC_NEWPNR 
   WHERE OBJEKTID LIKE '7310.%' AND LEN(OBJEKTID) > 11 AND RIGHT(OBJEKTID,11) = @VC_OLDPNR
  UPDATE GA1P0TXT SET OBJEKTID = SUBSTRING(OBJEKTID, 1, LEN(OBJEKTID)-11) + @VC_NEWPNR 
   WHERE OBJEKTID LIKE '7282.%' AND LEN(OBJEKTID) > 11 AND RIGHT(OBJEKTID,11) = @VC_OLDPNR
  UPDATE GA1P0TXT SET OBJEKTID = SUBSTRING(OBJEKTID, 1, LEN(OBJEKTID)-11) + @VC_NEWPNR 
   WHERE OBJEKTID LIKE '7290.%' AND LEN(OBJEKTID) > 11 AND RIGHT(OBJEKTID,11) = @VC_OLDPNR
  UPDATE LD0P0PDF SET OBJEKTNYCKEL = @VC_NEWPNR+SUBSTRING(OBJEKTNYCKEL,(CHARINDEX(';',OBJEKTNYCKEL)),LEN(OBJEKTNYCKEL)-(CHARINDEX(';',OBJEKTNYCKEL)-1)) 
   WHERE OBJEKTNYCKEL LIKE @VC_OLDPNR+'%'
  INSERT INTO LD0P0A02 (OLDPNR, NEWPNR, UPDDAT, UPDSIGN) VALUES
   (@VC_OLDPNR, @VC_NEWPNR, @VC_UPDDAT, @VC_UPDSIGN)
END; 
