CREATE TABLE LD0P0D03
(
	BERAKNR		DECIMAL(9)  NOT NULL ,
	UTGNR		DECIMAL(9)  NOT NULL ,
	TYP			VARCHAR(50)  NOT NULL ,
	ANSBEL		DECIMAL(9,2)  NOT NULL ,
	GODBEL		DECIMAL(9,2)  NOT NULL ,
	ANM			VARCHAR(80)  NOT NULL ,
	ANSBEL2		DECIMAL(9,2)  NOT NULL ,
	GODBEL2		DECIMAL(9,2)  NOT NULL ,
	JUSTERAEJ	DECIMAL(1)  NOT NULL ,
	UTGNR2		DECIMAL(9)  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_LD0P0D03 PRIMARY KEY NONCLUSTERED 
	   (
       BERAKNR,
       UTGNR  
	   ),
    PERIOD FOR SYSTEM_TIME (STARTTIME, ENDTIME) 
)
GO

ALTER TABLE LD0P0D03 ADD  CONSTRAINT LD0P0D03_STARTTIME  DEFAULT (getutcdate()) FOR STARTTIME
GO
ALTER TABLE LD0P0D03 ADD  CONSTRAINT LD0P0D03_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='LD0P0D03')
SET @SQL='ALTER TABLE LD0P0D03
SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE='+@SCHEMA+'.LD0L0D03))'
EXEC (@SQL)
END
GO

IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME='LD0V0D03GET')
BEGIN
DROP VIEW LD0V0D03GET
END
GO
CREATE VIEW LD0V0D03GET AS SELECT *, ENDTIME FROM LD0P0D03
GO
GRANT ALL ON LD0V0D03GET TO LD0ROANV
GO