CREATE TABLE LD0P0E
(
	BESLUTNR  DECIMAL(9)  NOT NULL ,
	INSNR  DECIMAL(9)  NOT NULL ,
	AEVERKNR  DECIMAL(9)  NOT NULL ,
	BESLNR2  DECIMAL(9)  NOT NULL ,
	BESLUT  DECIMAL(9)  NOT NULL ,
	BESLTYP  DECIMAL(9)  NOT NULL ,
	DATUM  VARCHAR(8)  NOT NULL ,
	FOMDAT  VARCHAR(8)  NOT NULL ,
	TOMDAT  VARCHAR(8)  NOT NULL ,
	ORSAK  DECIMAL(9)  NOT NULL ,
	BESFATT  VARCHAR(10)  NOT NULL ,
	BELOPP  DECIMAL(9,2)  NOT NULL ,
	PKLTYP  VARCHAR(2)  NOT NULL ,
	ENGBEL  DECIMAL(9,2)  NOT NULL ,
	GEMVARD  DECIMAL(1)  NOT NULL ,
	UPDSIGN  VARCHAR(10)  NOT NULL ,
	UPDDAT  VARCHAR(6)  NOT NULL ,
	REGDAT  VARCHAR(8)  NOT NULL ,
	FORMAVS  DECIMAL(1)  NOT NULL ,
	BARNVAEG  DECIMAL(9)  NOT NULL ,
	ORGID  DECIMAL(9)  NOT NULL ,
	ORGENHNR  VARCHAR(10)  NOT NULL ,
	VHAND  VARCHAR(10)  NOT NULL ,
	REFBESLUTNR  DECIMAL(9)  NOT NULL ,
	STATUS  DECIMAL(1)  NOT NULL ,
	HAND  VARCHAR(10)  NOT NULL ,
	MEDHAND  VARCHAR(10)  NOT NULL ,
	ORSAKMED  DECIMAL(9)  NOT NULL ,
	BIDRTFNR  DECIMAL(9)  NOT NULL ,
	REGSIGN  VARCHAR(10)  NOT NULL ,
	LAEGE  DECIMAL(1)  NOT NULL ,
	SPARRDAT  VARCHAR(8)  NOT NULL ,
	SPARRSIGN  VARCHAR(10)  NOT NULL ,
	SPARRORS  DECIMAL(9)  NOT NULL ,
	INBLANCO  DECIMAL(1)  NOT NULL ,
	ORGENHNR2  VARCHAR(10)  NOT NULL ,
	ORSMEDSAKN  VARCHAR(250)  NOT NULL ,
	BESLNRAR  VARCHAR(4)  NOT NULL ,
	BESLNRLNR  DECIMAL(9)  NOT NULL ,
	MAKDAT  VARCHAR(8)  NOT NULL ,
	MAKSIGN  VARCHAR(10)  NOT NULL ,
	STATORS1  DECIMAL(9)  NOT NULL ,
	STATORS2  DECIMAL(9)  NOT NULL ,
	STATORS3  DECIMAL(9)  NOT NULL ,
	STATORS4  DECIMAL(9)  NOT NULL ,
	STATORS5  DECIMAL(9)  NOT NULL ,
	STATORS6  DECIMAL(9)  NOT NULL ,
	STATORS7  DECIMAL(9)  NOT NULL ,
	STATORS8  DECIMAL(9)  NOT NULL ,
	VARKOMBARN  DECIMAL(9)  NOT NULL ,
	DELEGATION  DECIMAL(9)  NOT NULL ,
	BESFATTORG  VARCHAR(10)  NOT NULL ,
	INSTNR  DECIMAL(9)  NOT NULL ,
	FAMHEM  VARCHAR(80)  NOT NULL ,
	FRITTBEL  DECIMAL(1)  NOT NULL ,
	UPDTID  VARCHAR(6)  NOT NULL ,
	FORFAVSER  VARCHAR(100)  NOT NULL ,
	SUPPORTGRP  DECIMAL(1)  NOT NULL ,
	PLACTYP  DECIMAL(9)  NOT NULL ,
	ANNATTXT  VARCHAR(80)  NOT NULL ,
	AVSLUPDDAT  VARCHAR(8)  NOT NULL ,
	AVSLUPDSIGN  VARCHAR(10)  NOT NULL ,
	VERKSEJ  DECIMAL(1)  NOT NULL ,
	INSATGARD  DECIMAL(9)  NOT NULL ,
	INSATGARD2  DECIMAL(9)  NOT NULL ,
	GRUND  DECIMAL(1)  NOT NULL ,
	KOMPL  DECIMAL(1)  NOT NULL ,
	FOREBYGG  DECIMAL(1)  NOT NULL,
	ARBERS  DECIMAL(1)  NOT NULL,
	RESERS  DECIMAL(1)  NOT NULL,
	AKTUALNR  DECIMAL(9) DEFAULT  0 NOT NULL ,
	ORSAKSEN  DECIMAL(9) DEFAULT  0 NOT NULL ,
	LEDEREJTILLUPPDRAG DECIMAL(1) DEFAULT  0 NOT NULL ,
	INGENMEDSOK DECIMAL(1) DEFAULT 0 NOT NULL ,
	LEDEREJTILLAMUPPDRAG  DECIMAL(1) DEFAULT 0 NOT NULL ,
	LASBESLMEDD DECIMAL(1) DEFAULT 0 NOT NULL ,
	LIFECARE_CREATED DECIMAL(1) DEFAULT 0 NOT NULL ,
	VARDNHAV DECIMAL(9) DEFAULT 0 NOT NULL ,
	UNDANTAGSTATISTIK DECIMAL(1) NOT NULL,
  BFTJANST VARCHAR(80) DEFAULT ' ' NOT NULL 
)
GO

ALTER TABLE LD0P0E
ADD CONSTRAINT LD0d0E_UNDANTAGSTATISTIK
DEFAULT 0 FOR UNDANTAGSTATISTIK
GO

IF NOT EXISTS(SELECT 1 FROM sys.objects WHERE OBJECT_NAME(parent_object_id)='LD0P0E' AND TYPE='PK')
BEGIN
ALTER TABLE LD0P0E
ADD CONSTRAINT PK_LD0P0E PRIMARY KEY NONCLUSTERED (BESLUTNR)
END
GO
IF NOT EXISTS(SELECT * FROM sys.columns WHERE  object_id = OBJECT_ID(N'LD0P0E') AND name = 'StartTime')
BEGIN
ALTER TABLE LD0P0E
ADD StartTime DATETIME2 GENERATED ALWAYS AS ROW START HIDDEN
   NOT NULL CONSTRAINT LD0P0E_StartTime DEFAULT GETUTCDATE()
   ,EndTime   DATETIME2 GENERATED ALWAYS AS ROW END HIDDEN
   NOT NULL CONSTRAINT LD0P0E_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='LD0P0E')
SET @SQL='ALTER TABLE LD0P0E
SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE='+@SCHEMA+'.LD0L0E))'
EXEC (@SQL)
END
GO

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

IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME = 'LD0V0ET')
BEGIN
DROP VIEW LD0V0ET
END
GO
CREATE VIEW LD0V0ET
AS
SELECT P.*,P.STARTTIME,P.ENDTIME
FROM LD0P0E V
INNER JOIN LD0P0E P on P.BESLUTNR = V.BESLUTNR AND P.ENDTIME='9999-12-31 23:59:59.9999999'
GO
GRANT ALL ON LD0V0ET TO LD0ROANV
GO