IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME='LD0V0CUSTOMERDOCUMENTWITHLINKEDDATA')
BEGIN
DROP VIEW LD0V0CUSTOMERDOCUMENTWITHLINKEDDATA
END
;
CREATE VIEW LD0V0CUSTOMERDOCUMENTWITHLINKEDDATA
AS
SELECT LD0P0CUSTOMERDOCUMENT.*,
LD0P0CUSTOMERDOCUMENTLINKEDDATA.ENCOUNTERID,
LD0P0CUSTOMERDOCUMENTLINKEDDATA.PROCESSID,
PM.SERVICECATEGORYOID,
PM.PROCESSTYPEOID,
PM.SERVICETYPEOID
FROM LD0P0CUSTOMERDOCUMENT 
INNER JOIN LD0P0CUSTOMERDOCUMENTLINKEDDATA ON LD0P0CUSTOMERDOCUMENT.ID = LD0P0CUSTOMERDOCUMENTLINKEDDATA.CUSTOMERDOCUMENTID 
INNER JOIN LD0V1PROCESSMETADATA PM ON PM.CASEID=LD0P0CUSTOMERDOCUMENT.CASEID
;