CREATE TABLE LD0P0PDF
(
	DOKUMENTID	VARCHAR(38)  NOT NULL,
	MALLNAMN  	VARCHAR(30)  NOT NULL ,
	BESKRIVNING  	VARCHAR(180) NOT NULL ,
	OBJEKT  	VARCHAR(80)  NOT NULL ,
	OBJEKTNYCKEL  	VARCHAR(40)  NOT NULL ,
	DATUM  		VARCHAR(8)   NOT NULL ,
	KLOCKSLAG  	VARCHAR(9)   NOT NULL ,
	SIGNATUR  	VARCHAR(10)  NOT NULL ,
	MALLTITEL  	VARCHAR(250)  NOT NULL ,
	TYPPDF  	DECIMAL(9)   NOT NULL ,
	JDTYPPDF  	DECIMAL(5)   NOT NULL ,
	JDDOKUMENTID  	DECIMAL(8)   NOT NULL ,
	UPPDRAGNR 	DECIMAL(9)   DEFAULT 0 NOT NULL,
	NAMNDSIGNATUR 	VARCHAR(10)  DEFAULT ' ' NOT NULL,
	NAMNDDATUM 	VARCHAR(8)   DEFAULT ' ' NOT NULL,
	NAMNDKLOCKSLAG 	VARCHAR(9)   DEFAULT ' ' NOT NULL,
	NAMNDRENSDATUM 	VARCHAR(8)   DEFAULT ' ' NOT NULL,
  	LNUPPDRAGNR    	DECIMAL(9)   DEFAULT 0   NOT NULL,
	EXTERNTSYSTEM	DECIMAL(1)   DEFAULT 0  NOT NULL,
	PLANAKTIVITETNR  DECIMAL(9)  DEFAULT 0  NOT NULL,
	UPPDRAGIFONR 	DECIMAL(9)   DEFAULT 0 NOT NULL,
	FSUPPGIFTNR 	DECIMAL(9)   DEFAULT 0 NOT NULL,
	READBYCLIENT	VARCHAR(20)   DEFAULT ' ' NOT NULL,
	RESTILLDNR  DECIMAL(9) DEFAULT 0  NOT NULL,
	DELUPPDRAGIFONR DECIMAL(9) DEFAULT 0  NOT NULL,
	GODKANNANDESTATUS DECIMAL(1) DEFAULT 0  NOT NULL,
	NOTAPPROVALTEXT VARCHAR (180) DEFAULT ' ' NOT NULL, 
  STARTTIME datetime2(7) GENERATED ALWAYS AS ROW START HIDDEN NOT NULL,
  ENDTIME datetime2(7) GENERATED ALWAYS AS ROW END HIDDEN NOT NULL,
  BESTALLNINGVOONR DECIMAL(9) DEFAULT 0 NOT NULL,
       CONSTRAINT PK_LD0P0PDF PRIMARY KEY NONCLUSTERED 
	    (
		  DOKUMENTID
	    ),
       PERIOD FOR SYSTEM_TIME (STARTTIME, ENDTIME)
)
GO

ALTER TABLE LD0P0PDF ADD  CONSTRAINT LD0P0PDF_STARTTIME  DEFAULT (SYSUTCDATETIME()) FOR STARTTIME
GO
ALTER TABLE LD0P0PDF ADD  CONSTRAINT LD0P0PDF_ENDTIME  DEFAULT (CONVERT(datetime2,'9999-12-31 23:59:59.9999999')) FOR ENDTIME
GO

BEGIN
DECLARE @SQL VARCHAR(2000)
DECLARE @SCHEMA VARCHAR(100)=(SELECT SCH.NAME
FROM SYS.TABLES TAB
JOIN SYS.SCHEMAS SCH
ON SCH.schema_id=TAB.schema_id
WHERE TAB.NAME='LD0P0PDF')
SET @SQL='ALTER TABLE LD0P0PDF
SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE='+@SCHEMA+'.LD0L0PDF))'
EXEC (@SQL)
END
GO



