IF NOT EXISTS(SELECT 1 FROM sys.objects WHERE OBJECT_NAME(parent_object_id) = 'LD0P0Q02' AND TYPE = 'PK')
BEGIN
ALTER TABLE LD0P0Q02
ADD CONSTRAINT PK_LD0P0Q02 PRIMARY KEY NONCLUSTERED (KODTYP, KOD, KODTYP2, KOD2)
END
GO
IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'LD0P0Q02') AND name = 'STARTTIME')
BEGIN
ALTER TABLE LD0P0Q02 
ADD STARTTIME DATETIME2 GENERATED ALWAYS AS ROW START HIDDEN
NOT NULL CONSTRAINT LD0P0Q02_STARTTIME DEFAULT SYSUTCDATETIME(),
ENDTIME DATETIME2 GENERATED ALWAYS AS ROW END HIDDEN
NOT NULL CONSTRAINT LD0P0Q02_ENDTIME DEFAULT CONVERT(DATETIME2, '9999-12-31 23:59:59.9999999'),
PERIOD FOR SYSTEM_TIME (STARTTIME, ENDTIME)
END
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='LD0P0Q02')
SET @SQL = 'ALTER TABLE LD0P0Q02
SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = '+@SCHEMA+'.LD0L0Q02))'
EXEC (@SQL)
END
GO
