
CREATE TABLE LD0P0D01 (
       BERAKNR              DECIMAL(9) NOT NULL,
       PNR                  VARCHAR(12) NOT NULL,
       NORMRADNR            DECIMAL(2) NOT NULL,
       BELOPP               DECIMAL(9,2) NOT NULL,
       AVFOMDAT             VARCHAR(8) NOT NULL,
       AVTOMDAT             VARCHAR(8) NOT NULL,
       LCHAVDR              DECIMAL(9) NOT NULL,
       AVDGR                DECIMAL(3) NOT NULL,
       STARTTIME datetime2(7) GENERATED ALWAYS AS ROW START HIDDEN NOT NULL,
	   ENDTIME datetime2(7) GENERATED ALWAYS AS ROW END HIDDEN NOT NULL,
       CONSTRAINT PK_LD0P0D01 PRIMARY KEY NONCLUSTERED 
	    (
       BERAKNR,
       PNR
	    ),
       PERIOD FOR SYSTEM_TIME (STARTTIME, ENDTIME)
);

ALTER TABLE LD0P0D01 ADD  CONSTRAINT LD0P0D01_STARTTIME  DEFAULT (getutcdate()) FOR STARTTIME
;
ALTER TABLE LD0P0D01 ADD  CONSTRAINT LD0P0D01_ENDTIME  DEFAULT (CONVERT(datetime2,'9999-12-31 23:59:59.9999999')) FOR ENDTIME
;

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='LD0P0D01')
SET @SQL='ALTER TABLE LD0P0D01
SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE='+@SCHEMA+'.LD0L0D01))'
EXEC (@SQL)
END
;