IF NOT EXISTS(SELECT 1 FROM sys.columns 
          WHERE Name = N'LCMEDREADONLY'
          AND Object_ID = Object_ID(N'LD0P0A'))
BEGIN
ALTER TABLE LD0P0A ADD
	LCMEDREADONLY DECIMAL(1) 
	CONSTRAINT LD0D0A_LCMEDREADONLY DEFAULT 0 NOT NULL
END
;
DROP VIEW LD0V0A;
DROP VIEW LD0V1A;
DROP VIEW LD0V2A;
DROP VIEW LD0V3A;
DROP VIEW LD0V4A;
DROP VIEW LD0V5A;
DROP VIEW LD0VYA;
DROP VIEW PCCV0A;

DROP TRIGGER LD0T0A_KUNDNUMMER
;
CREATE TRIGGER LD0T0A_KUNDNUMMER ON LD0P0A
INSTEAD OF INSERT AS 
DECLARE 
@MAX_KUNDNUMMER DECIMAL(9),
@PNR VARCHAR(12) 
SET @MAX_KUNDNUMMER=(SELECT MAX(KUNDNUMMER)+1 FROM LD0P0A) 
SET @PNR=(SELECT PNR FROM INSERTED)
IF @MAX_KUNDNUMMER IS NULL 
BEGIN 
	SET @MAX_KUNDNUMMER=1 
END 
INSERT INTO LD0P0A SELECT PNR
      ,NAMN
      ,ADRESS
      ,COADR
      ,POSTNR
      ,ORT
      ,HEMTEL
      ,ARBTEL
      ,KADRESS
      ,KCOADR
      ,KPOSTNR
      ,KORT
      ,KHEMTEL
      ,KUATDAT
      ,KFLYKTING
      ,CIVKOD
      ,CIVDAT
      ,MEDBSKAP
      ,MEDBDAT
      ,FODFORS
      ,FODLAN
      ,FODLAND
      ,OMRADE
      ,FOLKFORS
      ,FOLKLAN
      ,FOLKKOM
      ,FOLKDAT
      ,AVREGORS
      ,AVREGDAT
      ,ADRSKYDD
      ,HEMLIGTFN
      ,LCHAVDR
      ,HARHUSHALL
      ,AKTRAKN
      ,KLIENTRAKN
      ,RESURSRAKN
      ,UPDDAT
      ,UPDSIGN
      ,KUPDDAT
      ,KUPDSIGN
      ,KOMDEL
      ,BETJOMR
      ,NYCKKOD
      ,FODDAT
      ,KON
      ,REFRAKN
      ,PERSTEL
      ,MOBIL
      ,FAX
      ,EPOSTHEM
      ,EPOSTARB
      ,FODKOM
      ,BOKOM
      ,MODMAL
      ,KOMSPRAK
      ,ADRSPRAK
      ,FONETNAMN
      ,YRKE
      ,SMEKNAMN
      ,UTL_ADR
      ,UTL_POSTADR
      ,UTL_ADRLAND
      ,TIDKOMM
      ,GRUPP
      ,EGENCIVKOD
      ,EJAVIS
      ,SAKNBOST
      ,AVLDAT
      ,SPRAKTEXT
      ,LANDTEXT
      ,ADRESSDAT
      ,UTLANDTEXT
      ,AVISFLAGDAT
      ,KOMSPRAK2
      ,KOMSPRAK3
      ,INVANDAT
      ,TOLKBEHOV
      ,MINSIDA
      ,@MAX_KUNDNUMMER
      ,EJKATEGORISERING
      ,KONTAKTSATT
      ,KONTAKTSIGN
      ,KONTAKTDATUM
      ,UPPLYSNING
      ,SMSAVIS
      ,MAILAVIS
      ,LCMEDDELANDE
      ,MINSIDA_VUXNA
      ,SKYDDADFOLKBOKF
      ,MINSIDA_ARBETSMARKNAD
      ,PRIMART_SPRAK
      ,TESTPERSON
      ,LCMEDREADONLY FROM INSERTED
;