--LD0P0C
SELECT HIS.NAME INTO #HISTORY_NAME
FROM SYS.TABLES TAB
JOIN SYS.TABLES HIS
ON TAB.HISTORY_TABLE_ID=HIS.OBJECT_ID
WHERE TAB.NAME = 'LD0P0C'
GO
ALTER TABLE LD0P0C SET (SYSTEM_VERSIONING = OFF)
GO
DECLARE @HISTORY_NAME VARCHAR(200) = (SELECT
										NAME
									FROM #HISTORY_NAME)
DECLARE @SQL_DEL VARCHAR(2000) = 'DELETE FROM ' +@HISTORY_NAME+ ' WHERE NOT EXISTS (SELECT AEVERKNR FROM LD0P0C C
	WHERE C.AEVERKNR=' +@HISTORY_NAME+ '.AEVERKNR)'
EXEC(@SQL_DEL)

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='LD0P0C')

DECLARE @SQL_ALTER VARCHAR(2000)='ALTER TABLE LD0P0C
SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE='+@SCHEMA+'.' +@HISTORY_NAME+ '))'
EXEC (@SQL_ALTER)
GO
DROP TABLE #HISTORY_NAME
GO


--LD0P0C01
SELECT HIS.NAME INTO #HISTORY_NAME
FROM SYS.TABLES TAB
JOIN SYS.TABLES HIS
ON TAB.HISTORY_TABLE_ID=HIS.OBJECT_ID
WHERE TAB.NAME = 'LD0P0C01'
GO
ALTER TABLE LD0P0C01 SET (SYSTEM_VERSIONING = OFF)
GO
DECLARE @HISTORY_NAME VARCHAR(200) = (SELECT
										NAME
									FROM #HISTORY_NAME)
DECLARE @SQL_DEL VARCHAR(2000) = 'DELETE FROM ' +@HISTORY_NAME+ ' WHERE NOT EXISTS (SELECT * FROM LD0P0C01 C01
	WHERE C01.AEVERKNR=' +@HISTORY_NAME+ '.AEVERKNR AND C01.PNR=' +@HISTORY_NAME+ '.PNR)'
EXEC(@SQL_DEL)

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='LD0P0C01')

DECLARE @SQL_ALTER VARCHAR(2000)='ALTER TABLE LD0P0C01
SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE='+@SCHEMA+'.' +@HISTORY_NAME+ '))'
EXEC (@SQL_ALTER)
GO
DROP TABLE #HISTORY_NAME
GO


--LD0P0C20
SELECT HIS.NAME INTO #HISTORY_NAME
FROM SYS.TABLES TAB
JOIN SYS.TABLES HIS
ON TAB.HISTORY_TABLE_ID=HIS.OBJECT_ID
WHERE TAB.NAME = 'LD0P0C20'
GO
ALTER TABLE LD0P0C20 SET (SYSTEM_VERSIONING = OFF)
GO
DECLARE @HISTORY_NAME VARCHAR(200) = (SELECT
										NAME
									FROM #HISTORY_NAME)
DECLARE @SQL_DEL VARCHAR(2000) = 'DELETE FROM ' +@HISTORY_NAME+ ' WHERE NOT EXISTS (SELECT * FROM LD0P0C20 C20
	WHERE C20.INSNR=' +@HISTORY_NAME+ '.INSNR)'
EXEC(@SQL_DEL)

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='LD0P0C20')

DECLARE @SQL_ALTER VARCHAR(2000)='ALTER TABLE LD0P0C20
SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE='+@SCHEMA+'.' +@HISTORY_NAME+ '))'
EXEC (@SQL_ALTER)
GO
DROP TABLE #HISTORY_NAME
GO


--LD0P0C21
SELECT HIS.NAME INTO #HISTORY_NAME
FROM SYS.TABLES TAB
JOIN SYS.TABLES HIS
ON TAB.HISTORY_TABLE_ID=HIS.OBJECT_ID
WHERE TAB.NAME = 'LD0P0C21'
GO
ALTER TABLE LD0P0C21 SET (SYSTEM_VERSIONING = OFF)
GO
DECLARE @HISTORY_NAME VARCHAR(200) = (SELECT
										NAME
									FROM #HISTORY_NAME)
DECLARE @SQL_DEL VARCHAR(2000) = 'DELETE FROM ' +@HISTORY_NAME+ ' WHERE NOT EXISTS (SELECT * FROM LD0P0C21 C21
	WHERE C21.INSNR=' +@HISTORY_NAME+ '.INSNR AND C21.PNR=' +@HISTORY_NAME+ '.PNR)'
EXEC(@SQL_DEL)

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='LD0P0C21')

DECLARE @SQL_ALTER VARCHAR(2000)='ALTER TABLE LD0P0C21
SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE='+@SCHEMA+'.' +@HISTORY_NAME+ '))'
EXEC (@SQL_ALTER)
GO
DROP TABLE #HISTORY_NAME
GO


--LD0P0D
SELECT HIS.NAME INTO #HISTORY_NAME
FROM SYS.TABLES TAB
JOIN SYS.TABLES HIS
ON TAB.HISTORY_TABLE_ID=HIS.OBJECT_ID
WHERE TAB.NAME = 'LD0P0D'
GO
ALTER TABLE LD0P0D SET (SYSTEM_VERSIONING = OFF)
GO
DECLARE @HISTORY_NAME VARCHAR(200) = (SELECT
										NAME
									FROM #HISTORY_NAME)
DECLARE @SQL_DEL VARCHAR(2000) = 'DELETE FROM ' +@HISTORY_NAME+ ' WHERE NOT EXISTS (SELECT * FROM LD0P0D D
	WHERE D.BERAKNR=' +@HISTORY_NAME+ '.BERAKNR)'
EXEC(@SQL_DEL)

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='LD0P0D')

DECLARE @SQL_ALTER VARCHAR(2000)='ALTER TABLE LD0P0D
SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE='+@SCHEMA+'.' +@HISTORY_NAME+ '))'
EXEC (@SQL_ALTER)
GO
DROP TABLE #HISTORY_NAME
GO

--LD0P0D01
SELECT HIS.NAME INTO #HISTORY_NAME
FROM SYS.TABLES TAB
JOIN SYS.TABLES HIS
ON TAB.HISTORY_TABLE_ID=HIS.OBJECT_ID
WHERE TAB.NAME = 'LD0P0D01'
GO
ALTER TABLE LD0P0D01 SET (SYSTEM_VERSIONING = OFF)
GO
DECLARE @HISTORY_NAME VARCHAR(200) = (SELECT
										NAME
									FROM #HISTORY_NAME)
DECLARE @SQL_DEL VARCHAR(2000) = 'DELETE FROM ' +@HISTORY_NAME+ ' WHERE NOT EXISTS (SELECT * FROM LD0P0D01 D01
	WHERE D01.BERAKNR=' +@HISTORY_NAME+ '.BERAKNR AND D01.PNR=' +@HISTORY_NAME+ '.PNR)'
EXEC(@SQL_DEL)

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')

DECLARE @SQL_ALTER VARCHAR(2000)='ALTER TABLE LD0P0D01
SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE='+@SCHEMA+'.' +@HISTORY_NAME+ '))'
EXEC (@SQL_ALTER)
GO
DROP TABLE #HISTORY_NAME
GO

--LD0P0D02
SELECT HIS.NAME INTO #HISTORY_NAME
FROM SYS.TABLES TAB
JOIN SYS.TABLES HIS
ON TAB.HISTORY_TABLE_ID=HIS.OBJECT_ID
WHERE TAB.NAME = 'LD0P0D02'
GO
ALTER TABLE LD0P0D02 SET (SYSTEM_VERSIONING = OFF)
GO
DECLARE @HISTORY_NAME VARCHAR(200) = (SELECT
										NAME
									FROM #HISTORY_NAME)
DECLARE @SQL_DEL VARCHAR(2000) = 'DELETE FROM ' +@HISTORY_NAME+ ' WHERE NOT EXISTS (SELECT * FROM LD0P0D02 D02
	WHERE D02.BERAKNR=' +@HISTORY_NAME+ '.BERAKNR AND D02.INKNR=' +@HISTORY_NAME+ '.INKNR)'
EXEC(@SQL_DEL)

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='LD0P0D02')

DECLARE @SQL_ALTER VARCHAR(2000)='ALTER TABLE LD0P0D02
SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE='+@SCHEMA+'.' +@HISTORY_NAME+ '))'
EXEC (@SQL_ALTER)
GO
DROP TABLE #HISTORY_NAME
GO


--LD0P0D03
SELECT HIS.NAME INTO #HISTORY_NAME
FROM SYS.TABLES TAB
JOIN SYS.TABLES HIS
ON TAB.HISTORY_TABLE_ID=HIS.OBJECT_ID
WHERE TAB.NAME = 'LD0P0D03'
GO
ALTER TABLE LD0P0D03 SET (SYSTEM_VERSIONING = OFF)
GO
DECLARE @HISTORY_NAME VARCHAR(200) = (SELECT
										NAME
									FROM #HISTORY_NAME)
DECLARE @SQL_DEL VARCHAR(2000) = 'DELETE FROM ' +@HISTORY_NAME+ ' WHERE NOT EXISTS (SELECT * FROM LD0P0D03 D03
	WHERE D03.BERAKNR=' +@HISTORY_NAME+ '.BERAKNR AND D03.UTGNR=' +@HISTORY_NAME+ '.UTGNR)'
EXEC(@SQL_DEL)

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')

DECLARE @SQL_ALTER VARCHAR(2000)='ALTER TABLE LD0P0D03
SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE='+@SCHEMA+'.' +@HISTORY_NAME+ '))'
EXEC (@SQL_ALTER)
GO
DROP TABLE #HISTORY_NAME
GO


--LD0P0D04
SELECT HIS.NAME INTO #HISTORY_NAME
FROM SYS.TABLES TAB
JOIN SYS.TABLES HIS
ON TAB.HISTORY_TABLE_ID=HIS.OBJECT_ID
WHERE TAB.NAME = 'LD0P0D04'
GO
ALTER TABLE LD0P0D04 SET (SYSTEM_VERSIONING = OFF)
GO
DECLARE @HISTORY_NAME VARCHAR(200) = (SELECT
										NAME
									FROM #HISTORY_NAME)
DECLARE @SQL_DEL VARCHAR(2000) = 'DELETE FROM ' +@HISTORY_NAME+ ' WHERE NOT EXISTS (SELECT * FROM LD0P0D04 D04
	WHERE D04.BERAKNR=' +@HISTORY_NAME+ '.BERAKNR AND D04.SPECNR=' +@HISTORY_NAME+ '.SPECNR)'
EXEC(@SQL_DEL)

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='LD0P0D04')

DECLARE @SQL_ALTER VARCHAR(2000)='ALTER TABLE LD0P0D04
SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE='+@SCHEMA+'.' +@HISTORY_NAME+ '))'
EXEC (@SQL_ALTER)
GO
DROP TABLE #HISTORY_NAME
GO


--LD0P0E
SELECT HIS.NAME INTO #HISTORY_NAME
FROM SYS.TABLES TAB
JOIN SYS.TABLES HIS
ON TAB.HISTORY_TABLE_ID=HIS.OBJECT_ID
WHERE TAB.NAME = 'LD0P0E'
GO
ALTER TABLE LD0P0E SET (SYSTEM_VERSIONING = OFF)
GO
DECLARE @HISTORY_NAME VARCHAR(200) = (SELECT
										NAME
									FROM #HISTORY_NAME)
DECLARE @SQL_DEL VARCHAR(2000) = 'DELETE FROM ' +@HISTORY_NAME+ ' WHERE NOT EXISTS (SELECT * FROM LD0P0E E
	WHERE E.BESLUTNR=' +@HISTORY_NAME+ '.BESLUTNR)'
EXEC(@SQL_DEL)

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='LD0P0E')

DECLARE @SQL_ALTER VARCHAR(2000)='ALTER TABLE LD0P0E
SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE='+@SCHEMA+'.' +@HISTORY_NAME+ '))'
EXEC (@SQL_ALTER)
GO
DROP TABLE #HISTORY_NAME
GO


--LD0P0E01
SELECT HIS.NAME INTO #HISTORY_NAME
FROM SYS.TABLES TAB
JOIN SYS.TABLES HIS
ON TAB.HISTORY_TABLE_ID=HIS.OBJECT_ID
WHERE TAB.NAME = 'LD0P0E01'
GO
ALTER TABLE LD0P0E01 SET (SYSTEM_VERSIONING = OFF)
GO
DECLARE @HISTORY_NAME VARCHAR(200) = (SELECT
										NAME
									FROM #HISTORY_NAME)
DECLARE @SQL_DEL VARCHAR(2000) = 'DELETE FROM ' +@HISTORY_NAME+ ' WHERE NOT EXISTS (SELECT * FROM LD0P0E01 E01
	WHERE E01.BESLUTNR=' +@HISTORY_NAME+ '.BESLUTNR and E01.PNR=' +@HISTORY_NAME+ '.PNR)'
EXEC(@SQL_DEL)

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='LD0P0E01')

DECLARE @SQL_ALTER VARCHAR(2000)='ALTER TABLE LD0P0E01
SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE='+@SCHEMA+'.' +@HISTORY_NAME+ '))'
EXEC (@SQL_ALTER)
GO
DROP TABLE #HISTORY_NAME
GO


--LD0P0F
SELECT HIS.NAME INTO #HISTORY_NAME
FROM SYS.TABLES TAB
JOIN SYS.TABLES HIS
ON TAB.HISTORY_TABLE_ID=HIS.OBJECT_ID
WHERE TAB.NAME = 'LD0P0F'
GO
ALTER TABLE LD0P0F SET (SYSTEM_VERSIONING = OFF)
GO
DECLARE @HISTORY_NAME VARCHAR(200) = (SELECT
										NAME
									FROM #HISTORY_NAME)
DECLARE @SQL_DEL VARCHAR(2000) = 'DELETE FROM ' +@HISTORY_NAME+ ' WHERE NOT EXISTS (SELECT * FROM LD0P0F F
	WHERE F.BETALNR=' +@HISTORY_NAME+ '.BETALNR)'
EXEC(@SQL_DEL)

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='LD0P0F')

DECLARE @SQL_ALTER VARCHAR(2000)='ALTER TABLE LD0P0F
SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE='+@SCHEMA+'.' +@HISTORY_NAME+ '))'
EXEC (@SQL_ALTER)
GO
DROP TABLE #HISTORY_NAME
GO


--LD0P0F01
SELECT HIS.NAME INTO #HISTORY_NAME
FROM SYS.TABLES TAB
JOIN SYS.TABLES HIS
ON TAB.HISTORY_TABLE_ID=HIS.OBJECT_ID
WHERE TAB.NAME = 'LD0P0F01'
GO
ALTER TABLE LD0P0F01 SET (SYSTEM_VERSIONING = OFF)
GO
DECLARE @HISTORY_NAME VARCHAR(200) = (SELECT
										NAME
									FROM #HISTORY_NAME)
DECLARE @SQL_DEL VARCHAR(2000) = 'DELETE FROM ' +@HISTORY_NAME+ ' WHERE NOT EXISTS (SELECT * FROM LD0P0F01 F01
	WHERE F01.BETALNR=' +@HISTORY_NAME+ '.BETALNR AND F01.PNR=' +@HISTORY_NAME+ '.PNR)'
EXEC(@SQL_DEL)

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='LD0P0F01')

DECLARE @SQL_ALTER VARCHAR(2000)='ALTER TABLE LD0P0F01
SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE='+@SCHEMA+'.' +@HISTORY_NAME+ '))'
EXEC (@SQL_ALTER)
GO
DROP TABLE #HISTORY_NAME
GO

--LD0P0H
SELECT HIS.NAME INTO #HISTORY_NAME
FROM SYS.TABLES TAB
JOIN SYS.TABLES HIS
ON TAB.HISTORY_TABLE_ID=HIS.OBJECT_ID
WHERE TAB.NAME = 'LD0P0H'
GO
ALTER TABLE LD0P0H SET (SYSTEM_VERSIONING = OFF)
GO
DECLARE @HISTORY_NAME VARCHAR(200) = (SELECT
										NAME
									FROM #HISTORY_NAME)
DECLARE @SQL_DEL VARCHAR(2000) = 'DELETE FROM ' +@HISTORY_NAME+ ' WHERE NOT EXISTS (SELECT * FROM LD0P0H H
	WHERE H.AKTUALNR='+@HISTORY_NAME+'.AKTUALNR)'
EXEC(@SQL_DEL)

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='LD0P0H')

DECLARE @SQL_ALTER VARCHAR(2000)='ALTER TABLE LD0P0H
SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE='+@SCHEMA+'.' +@HISTORY_NAME+ '))'
EXEC (@SQL_ALTER)
GO
DROP TABLE #HISTORY_NAME
GO


--LD0P0N70
SELECT HIS.NAME INTO #HISTORY_NAME
FROM SYS.TABLES TAB
JOIN SYS.TABLES HIS
ON TAB.HISTORY_TABLE_ID=HIS.OBJECT_ID
WHERE TAB.NAME = 'LD0P0N70'
GO
ALTER TABLE LD0P0N70 SET (SYSTEM_VERSIONING = OFF)
GO
DECLARE @HISTORY_NAME VARCHAR(200) = (SELECT
										NAME
									FROM #HISTORY_NAME)
DECLARE @SQL_DEL VARCHAR(2000) = 'DELETE FROM ' +@HISTORY_NAME+ ' WHERE NOT EXISTS (SELECT * FROM LD0P0N70 N70
	WHERE N70.RESTILLDNR='+@HISTORY_NAME+'.RESTILLDNR)'
EXEC(@SQL_DEL)

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='LD0P0N70')

DECLARE @SQL_ALTER VARCHAR(2000)='ALTER TABLE LD0P0N70
SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE='+@SCHEMA+'.' +@HISTORY_NAME+ '))'
EXEC (@SQL_ALTER)
GO
DROP TABLE #HISTORY_NAME
GO