CREATE TABLE LD0P0F
(
	BETALNR  DECIMAL(9)  NOT NULL ,
	INSNR  DECIMAL(9)  NOT NULL ,
	INBTYP  DECIMAL(9)  NOT NULL ,
	BELOPP  DECIMAL(9,2)  NOT NULL ,
	BETTYP  DECIMAL(9)  NOT NULL ,
	KEDJENR  DECIMAL(9)  NOT NULL ,
	AEVERKNR  DECIMAL(9)  NOT NULL ,
	AGARNR  DECIMAL(9)  NOT NULL ,
	AGARTYP  DECIMAL(3)  NOT NULL ,
	BOKDAT  VARCHAR(8)  NOT NULL ,
	BETDAT  VARCHAR(8)  NOT NULL ,
	FEL  DECIMAL(3)  NOT NULL ,
	INTDAT  VARCHAR(8)  NOT NULL ,
	MAKDAT  VARCHAR(8)  NOT NULL ,
	UPDDAT  VARCHAR(6)  NOT NULL ,
	UPDSIGN  VARCHAR(10)  NOT NULL ,
	STATUS  DECIMAL(2)  NOT NULL ,
	EJINT  DECIMAL(1)  NOT NULL ,
	LOPNR  DECIMAL(9)  NOT NULL ,
	VERNR  DECIMAL(11)  NOT NULL ,
	EKENHNR  DECIMAL(9)  NOT NULL ,
	BETSATT  DECIMAL(9)  NOT NULL ,
	CLEAR  VARCHAR(5)  NOT NULL ,
	KONTONR  VARCHAR(40)  NOT NULL ,
	NAMN  VARCHAR(36)  NOT NULL ,
	ADRESS  VARCHAR(35)  NOT NULL ,
	COADR  VARCHAR(35)  NOT NULL ,
	POSTNR  VARCHAR(5)  NOT NULL ,
	ORT  VARCHAR(27)  NOT NULL ,
	RAKNNR  VARCHAR(25)  NOT NULL ,
	LOKBET  VARCHAR(10)  NOT NULL ,
	BETMED  VARCHAR(250)  NOT NULL ,
	OCR  DECIMAL(1)  NOT NULL ,
	BESFATT  VARCHAR(10)  NOT NULL ,
	AVISERING  DECIMAL(1)  NOT NULL ,
	REFERENSNR  VARCHAR(25)  NOT NULL ,
	BIDRTFNR  DECIMAL(9)  NOT NULL ,
	MAKSIGN  VARCHAR(10)  NOT NULL ,
	FORFAVSER  VARCHAR(100)  NOT NULL ,
	FORFFROM  VARCHAR(8)  NOT NULL ,
	FORFTOM  VARCHAR(8)  NOT NULL ,
	FORFAVSAND  VARCHAR(100)  NOT NULL ,
	FORFATERB  DECIMAL(9,2)  NOT NULL ,
	EPPBREV  DECIMAL(1)  NOT NULL ,
	ORSJUST  DECIMAL(9)  NOT NULL ,
	NOTERING  VARCHAR(250)  NOT NULL ,
	NOTIFIERA  DECIMAL(3)  NOT NULL ,
	ANULLEDTYPE  DECIMAL(1)  NOT NULL ,
	BCORRDATE  VARCHAR(8)  NOT NULL ,
	RETURBELOPP  DECIMAL(9,2)  NOT NULL ,
	FPACREATED  VARCHAR(8)  NOT NULL ,
	KONTONR_OLD  VARCHAR(35)  NOT NULL ,
	EGNAMEDEL  DECIMAL(9,2)  NOT NULL ,
	AVSERMANAD VARCHAR(6) NOT NULL , 
	KOSTDAT VARCHAR(8) DEFAULT ' ' NOT NULL,
	AKTUALNR	DECIMAL(9) DEFAULT  0 NOT NULL,
	BUNTNR DECIMAL(9) DEFAULT  0 NOT NULL,
	VISASEJ_PA_MINSIDA  DECIMAL(1) DEFAULT 0 NOT NULL
);

IF NOT EXISTS(SELECT 1 FROM sys.objects WHERE OBJECT_NAME(parent_object_id)='LD0P0F' AND TYPE='PK')
BEGIN
ALTER TABLE LD0P0F
ADD CONSTRAINT PK_LD0P0F PRIMARY KEY NONCLUSTERED (BETALNR)
END
;
IF NOT EXISTS(SELECT * FROM sys.columns WHERE  object_id = OBJECT_ID(N'LD0P0F') AND name = 'StartTime')
BEGIN
ALTER TABLE LD0P0F
ADD StartTime DATETIME2 GENERATED ALWAYS AS ROW START HIDDEN
   NOT NULL CONSTRAINT LD0P0F_StartTime DEFAULT GETUTCDATE()
   ,EndTime   DATETIME2 GENERATED ALWAYS AS ROW END HIDDEN
   NOT NULL CONSTRAINT LD0P0F_EndTime DEFAULT CONVERT(DATETIME2, '9999-12-31 23:59:59.9999999'),
PERIOD FOR SYSTEM_TIME (StartTime, EndTime)
END
;
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='LD0P0F')
SET @SQL='ALTER TABLE LD0P0F
SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE='+@SCHEMA+'.LD0L0F))'
EXEC (@SQL)
END
;

