IF NOT EXISTS(SELECT 1 FROM sys.columns 
          WHERE Name = N'DATUM'
          AND Object_ID = Object_ID(N'LD0P0DecisionProposal'))
BEGIN
ALTER TABLE LD0P0DecisionProposal ADD 
	  DATUM VARCHAR(8)
    CONSTRAINT LD0D0DECISIONPROPOSAL_DATUM DEFAULT ((' ')) NOT NULL
END
GO

IF NOT EXISTS(SELECT 1 FROM sys.columns 
          WHERE Name = N'FOMDAT'
          AND Object_ID = Object_ID(N'LD0P0DecisionProposal'))
BEGIN
ALTER TABLE LD0P0DecisionProposal ADD 
    FOMDAT VARCHAR(8)
    CONSTRAINT LD0D0DECISIONPROPOSAL_FOMDAT DEFAULT ((' ')) NOT NULL
END
GO

IF NOT EXISTS(SELECT 1 FROM sys.columns 
          WHERE Name = N'TOMDAT'
          AND Object_ID = Object_ID(N'LD0P0DecisionProposal'))
BEGIN
ALTER TABLE LD0P0DecisionProposal ADD 
    TOMDAT VARCHAR(8)
    CONSTRAINT LD0D0DECISIONPROPOSAL_TOMDAT DEFAULT ((' ')) NOT NULL
END
GO

IF NOT EXISTS(SELECT 1 FROM sys.columns 
          WHERE Name = N'ORSAK'
          AND Object_ID = Object_ID(N'LD0P0DecisionProposal'))
BEGIN
ALTER TABLE LD0P0DecisionProposal ADD 
    ORSAK DECIMAL(9)
    CONSTRAINT LD0D0DECISIONPROPOSAL_ORSAK DEFAULT 0 NOT NULL
END
GO

IF NOT EXISTS(SELECT 1 FROM sys.columns 
          WHERE Name = N'ORSAKMED'
          AND Object_ID = Object_ID(N'LD0P0DecisionProposal'))
BEGIN
ALTER TABLE LD0P0DecisionProposal ADD 
    ORSAKMED DECIMAL(9)
    CONSTRAINT LD0D0DECISIONPROPOSAL_ORSAKMED DEFAULT 0 NOT NULL
END
GO

IF NOT EXISTS(SELECT 1 FROM sys.columns 
          WHERE Name = N'BELOPP'
          AND Object_ID = Object_ID(N'LD0P0DecisionProposal'))
BEGIN
ALTER TABLE LD0P0DecisionProposal ADD 
    BELOPP DECIMAL(9, 2)
    CONSTRAINT LD0D0DECISIONPROPOSAL_BELOPP DEFAULT 0 NOT NULL
END
GO

IF NOT EXISTS(SELECT 1 FROM sys.columns 
          WHERE Name = N'SENDER'
          AND Object_ID = Object_ID(N'LD0P0DecisionProposal'))
BEGIN
ALTER TABLE LD0P0DecisionProposal ADD 
    SENDER VARCHAR(10)
    CONSTRAINT LD0D0DECISIONPROPOSAL_SENDER DEFAULT ((' ')) NOT NULL
END
GO

IF NOT EXISTS(SELECT 1 FROM sys.columns 
          WHERE Name = N'RECEIVER'
          AND Object_ID = Object_ID(N'LD0P0DecisionProposal'))
BEGIN
ALTER TABLE LD0P0DecisionProposal ADD 
    RECEIVER VARCHAR(10)
    CONSTRAINT LD0D0DECISIONPROPOSAL_RECEIVER DEFAULT ((' ')) NOT NULL
END
GO

IF NOT EXISTS(SELECT 1 FROM sys.columns 
          WHERE Name = N'INFO'
          AND Object_ID = Object_ID(N'LD0P0DecisionProposal'))
BEGIN
ALTER TABLE LD0P0DecisionProposal ADD 
    INFO VARCHAR(250)
    CONSTRAINT LD0D0DECISIONPROPOSAL_INFO DEFAULT ((' ')) NOT NULL
END
GO

IF NOT EXISTS(SELECT 1 FROM sys.columns 
          WHERE Name = N'BARNVAEG'
          AND Object_ID = Object_ID(N'LD0P0DecisionProposal'))
BEGIN
ALTER TABLE LD0P0DecisionProposal ADD 
    BARNVAEG DECIMAL(9)
    CONSTRAINT LD0D0DECISIONPROPOSAL_BARNVAEG DEFAULT 0 NOT NULL
END
GO

IF NOT EXISTS(SELECT 1 FROM sys.columns 
          WHERE Name = N'VARDNHAV'
          AND Object_ID = Object_ID(N'LD0P0DecisionProposal'))
BEGIN
ALTER TABLE LD0P0DecisionProposal ADD 
    VARDNHAV DECIMAL(9)
    CONSTRAINT LD0D0DECISIONPROPOSAL_VARDNHAV DEFAULT 0 NOT NULL
END
GO

IF NOT EXISTS(SELECT 1 FROM sys.columns 
          WHERE Name = N'AKTUALNR'
          AND Object_ID = Object_ID(N'LD0P0DecisionProposal'))
BEGIN
ALTER TABLE LD0P0DecisionProposal ADD 
    AKTUALNR DECIMAL(9)
    CONSTRAINT LD0D0DECISIONPROPOSAL_AKTUALNR DEFAULT 0 NOT NULL
END
GO

CREATE OR ALTER VIEW LD0V0DecisionProposal AS SELECT * FROM LD0P0DecisionProposal
GO

CREATE OR ALTER VIEW LD0VAA0DECISIONPROPOSAL
AS SELECT
DP.*,
Q01_BESLUT.KLARTEXT BESLUT_T,
ISNULL(VI_SENDER.NAMN, '') SENDER_T,
ISNULL(V1_RECEIVER.NAMN, '') RECEIVER_T,
ISNULL(Q01_ORSAK.KLARTEXT, '') ORSAK_T,
ISNULL(Q01_MEDORSAK.KLARTEXT, '') MEDORSAK_T,
ISNULL(Q01_BARN.KLARTEXT, '') BARNVAEG_T,
ISNULL(Q01_VH.KLARTEXT, '') VARDNHAV_T
FROM
LD0V0DecisionProposal DP
LEFT JOIN LD0V0I VI_SENDER 
ON DP.SENDER = VI_SENDER.HAND
LEFT JOIN LD0V0I V1_RECEIVER
ON DP.RECEIVER = V1_RECEIVER.HAND
LEFT JOIN LD0V0Q01 Q01_ORSAK 
ON Q01_ORSAK.KODTYP = 'BESLORSAK' 
AND DP.ORSAK = Q01_ORSAK.KOD
INNER JOIN LD0V0Q01 Q01_BESLUT 
ON Q01_BESLUT.KODTYP = 'BESLUT' 
AND DP.DECISIONTYPE = Q01_BESLUT.KOD
LEFT JOIN LD0V0Q01 Q01_MEDORSAK 
ON Q01_MEDORSAK.KODTYP = 'BESLORSAK' 
AND DP.ORSAKMED = Q01_MEDORSAK.KOD
LEFT JOIN LD0V0Q01 Q01_BARN 
ON Q01_BARN.KODTYP = 'BARNVAEG' 
AND DP.BARNVAEG = Q01_BARN.KOD
LEFT JOIN LD0V0Q01 Q01_VH 
ON Q01_VH.KODTYP = 'VARDHAVARE' 
AND DP.VARDNHAV = Q01_VH.KOD
GO