@showStandard VARCHAR(250)

AS SELECT DISTINCT
    case when sclPrg.ProgramID is null then case when sclPrg1.ProgramID is null then Programs.ProgramID else sclPrg1.ProgramID end else sclPrg.ProgramID end as ProgramID,
    case when sclPrg.ProgramName is null then case when sclPrg1.ProgramName is null then DDCLValue.Value else sclPrg1.ProgramName end else sclPrg.ProgramName end as ProgramName,
    case when Programs.ProgramTypeID is null then 19 else Programs.ProgramTypeID end AS ProgramTypeId,
    DDCLElementParent.Id AS ParentId,
    case when Occurrences_2.StartRow is null then case when Occurrences_3.StartRow is null then Occurrences_4.StartRow else Occurrences_3.StartRow end else Occurrences_2.StartRow end AS ElementStartRow,
    case when Occurrences_2.EndRow is null then case when Occurrences_3.EndRow is null then Occurrences_4.EndRow else Occurrences_3.EndRow end else Occurrences_2.EndRow end AS ElementEndRow,
    case when Paths_2.PathStr is null then case when Paths_3.PathStr is null then Paths_4.PathStr else Paths_3.PathStr end else Paths_2.PathStr end AS ElementPathStr,
    CallSourceInfo.StartRow AS CallStartRow,
    CallSourceInfo.EndRow AS CallEndRow,
    Paths_1.PathStr AS CallPathStr,
    case when Programs_Ancestor.ProgramName is null then Programs_Ancestor1.ProgramName else Programs_Ancestor.ProgramName end AS AncestorName,
	case when Programs_Ancestor.ProgramID is null then Programs_Ancestor1.ProgramID else Programs_Ancestor.ProgramID end AS AncestorId,
	DDCLElementParent.DdsTypeId
FROM DDCLValue
    INNER JOIN DDCLProperty ON DDCLValue.PropertyId=DDCLProperty.Id
    INNER JOIN DDCLElement AS DDCLElementParent ON DDCLValue.ElementId=DDCLElementParent.Id
    LEFT JOIN Programs as sclPrg ON DDCLValue.Value=sclPrg.ProgramName  and sclPrg.ProgramTypeID=19
    left join SCLProcedureSynonyms ON sclPrg.ProgramName = SCLProcedureSynonyms.SCLProcedureName
    
	LEFT JOIN SCLProcedureSynonyms as sclProc ON DDCLValue.Value=sclProc.SCLProcedureSynonymName
	LEFT JOIN Programs as sclPrg1 on sclProc.SCLProcedureName=sclPrg1.ProgramName 
	
	LEFT JOIN Programs ON (DDCLValue.Value=Programs.ProgramName)
        AND (Programs.ProgramTypeID=1)
	
    LEFT JOIN Programs AS Programs_Ancestor ON sclPrg.Ancestor=Programs_Ancestor.ProgramName 
	AND Programs_Ancestor.ProgramTypeID=13
	LEFT JOIN Programs AS Programs_Ancestor1 ON sclPrg1.Ancestor=Programs_Ancestor1.ProgramName
	AND Programs_Ancestor1.ProgramTypeID=13

    INNER JOIN DDCLSourceInfo AS CallSourceInfo ON DDCLValue.SourceInfo=CallSourceInfo.Id
    INNER JOIN Programs AS Programs_2 ON CallSourceInfo.ProgramId=Programs_2.ProgramID
    INNER JOIN Occurrences AS Occurrences_1 ON Programs_2.OccurID=Occurrences_1.OccurID
    INNER JOIN Paths AS Paths_1 ON Occurrences_1.PathID=Paths_1.PathID
    
    /* SCL Procedures that are 'called' using aliases
    - the aliases are resolved into the actual procedures' names*/		
    LEFT JOIN Occurrences AS Occurrences_2 ON sclPrg.OccurID=Occurrences_2.OccurID
    LEFT JOIN Paths AS Paths_2 ON Occurrences_2.PathID=Paths_2.PathID
        
    /**SCL Procedures that are 'called' using the name of the SCL file containing the procedure
    - the name of the file is resoloved to the actual procedure's name*/
    LEFT JOIN Occurrences AS Occurrences_3 ON sclPrg1.OccurID=Occurrences_3.OccurID
    LEFT JOIN Paths AS Paths_3 ON Occurrences_3.PathID=Paths_3.PathID
        
    /*Cobol programs that are 'called'
    and that are NOT also present as SCL Standard procedures, SCL procedures or aliases*/
    LEFT JOIN Occurrences AS Occurrences_4 ON Programs.OccurID=Occurrences_4.OccurID
    LEFT JOIN Paths AS Paths_4 ON Occurrences_4.PathID=Paths_4.PathID
        
    LEFT JOIN SCLStandardProcedures AS Cobol_SCL_Std_Proc_Filter ON Programs.ProgramName=Cobol_SCL_Std_Proc_Filter.Name
    LEFT JOIN Programs AS Cobol_SCL_Proc_Filter ON Programs.ProgramName=Cobol_SCL_Proc_Filter.ProgramName
        AND Cobol_SCL_Proc_Filter.ProgramTypeID IN (13,19)
    LEFT JOIN SCLProcedureSynonyms AS Cobol_SCL_Proc_Alias_Filter ON Programs.ProgramName=Cobol_SCL_Proc_Alias_Filter.SCLProcedureSynonymName
    
    LEFT JOIN SCLStandardProcedures ON DDCLValue.Value=SCLStandardProcedures.Name
WHERE (DDCLProperty.Name in ('MODULE-ID','COMPONENT-ID','PROGRAM-ID'))
    AND (DDCLElementParent.DdsTypeId IN (4, 11, 27,28))
 	AND (DDCLElementParent.Id IN (SELECT * FROM #numeric_param_temp))
    AND (@showStandard='true'  or (SCLStandardProcedures.Name IS null))
    AND (Cobol_SCL_Std_Proc_Filter.Name IS NULL)
    AND (Cobol_SCL_Proc_Filter.ProgramTypeID IS NULL)
    AND (Cobol_SCL_Proc_Alias_Filter.SCLProcedureSynonymName IS NULL)

UNION    
SELECT DISTINCT
    case when sclPrg.ProgramID is null then case when sclPrg1.ProgramID is null then Programs.ProgramID else sclPrg1.ProgramID end else sclPrg.ProgramID end as ProgramID,
    case when sclPrg.ProgramName is null then case when sclPrg1.ProgramName is null then DDCLValue.ReferenceName else sclPrg1.ProgramName end else sclPrg.ProgramName end as ProgramName,
    case when Programs.ProgramTypeID is null then 19 else Programs.ProgramTypeID end AS ProgramTypeId,
    DDCLElementParent.Id AS ParentId,
    case when Occurrences_2.StartRow is null then case when Occurrences_3.StartRow is null then Occurrences_4.StartRow else Occurrences_3.StartRow end else Occurrences_2.StartRow end AS ElementStartRow,
    case when Occurrences_2.EndRow is null then case when Occurrences_3.EndRow is null then Occurrences_4.EndRow else Occurrences_3.EndRow end else Occurrences_2.EndRow end AS ElementEndRow,
    case when Paths_2.PathStr is null then case when Paths_3.PathStr is null then Paths_4.PathStr else Paths_3.PathStr end else Paths_2.PathStr end AS ElementPathStr,
    CallSourceInfo.StartRow AS CallStartRow,
    CallSourceInfo.EndRow AS CallEndRow,
    Paths_1.PathStr AS CallPathStr,
    case when Programs_Ancestor.ProgramName is null then Programs_Ancestor1.ProgramName else Programs_Ancestor.ProgramName end AS AncestorName,
	case when Programs_Ancestor.ProgramID is null then Programs_Ancestor1.ProgramID else Programs_Ancestor.ProgramID end AS AncestorId,
	DDCLElementParent.DdsTypeId
FROM DDCLValue
    INNER JOIN DDCLProperty ON DDCLValue.PropertyId=DDCLProperty.Id
    INNER JOIN DDCLElement AS DDCLElementParent ON DDCLValue.ElementId=DDCLElementParent.Id
    LEFT JOIN Programs as sclPrg ON DDCLValue.ReferenceName=sclPrg.ProgramName  and sclPrg.ProgramTypeID=19
    left join SCLProcedureSynonyms ON sclPrg.ProgramName = SCLProcedureSynonyms.SCLProcedureName
    
	LEFT JOIN SCLProcedureSynonyms as sclProc ON DDCLValue.ReferenceName=sclProc.SCLProcedureSynonymName
	LEFT JOIN Programs as sclPrg1 on sclProc.SCLProcedureName=sclPrg1.ProgramName 
	
	LEFT JOIN Programs ON (DDCLValue.ReferenceName=Programs.ProgramName)
        AND (Programs.ProgramTypeID=1)
	
    LEFT JOIN Programs AS Programs_Ancestor ON sclPrg.Ancestor=Programs_Ancestor.ProgramName 
	AND Programs_Ancestor.ProgramTypeID=13
	LEFT JOIN Programs AS Programs_Ancestor1 ON sclPrg1.Ancestor=Programs_Ancestor1.ProgramName
	AND Programs_Ancestor1.ProgramTypeID=13

    INNER JOIN DDCLSourceInfo AS CallSourceInfo ON DDCLValue.SourceInfo=CallSourceInfo.Id
    INNER JOIN Programs AS Programs_2 ON CallSourceInfo.ProgramId=Programs_2.ProgramID
    INNER JOIN Occurrences AS Occurrences_1 ON Programs_2.OccurID=Occurrences_1.OccurID
    INNER JOIN Paths AS Paths_1 ON Occurrences_1.PathID=Paths_1.PathID
    
    /* SCL Procedures that are 'called' using aliases
    - the aliases are resolved into the actual procedures' names*/		
    LEFT JOIN Occurrences AS Occurrences_2 ON sclPrg.OccurID=Occurrences_2.OccurID
    LEFT JOIN Paths AS Paths_2 ON Occurrences_2.PathID=Paths_2.PathID
        
    /**SCL Procedures that are 'called' using the name of the SCL file containing the procedure
    - the name of the file is resoloved to the actual procedure's name*/
    LEFT JOIN Occurrences AS Occurrences_3 ON sclPrg1.OccurID=Occurrences_3.OccurID
    LEFT JOIN Paths AS Paths_3 ON Occurrences_3.PathID=Paths_3.PathID
        
    /*Cobol programs that are 'called'
    and that are NOT also present as SCL Standard procedures, SCL procedures or aliases*/
    LEFT JOIN Occurrences AS Occurrences_4 ON Programs.OccurID=Occurrences_4.OccurID
    LEFT JOIN Paths AS Paths_4 ON Occurrences_4.PathID=Paths_4.PathID
        
    LEFT JOIN SCLStandardProcedures AS Cobol_SCL_Std_Proc_Filter ON Programs.ProgramName=Cobol_SCL_Std_Proc_Filter.Name
    LEFT JOIN Programs AS Cobol_SCL_Proc_Filter ON Programs.ProgramName=Cobol_SCL_Proc_Filter.ProgramName
        AND Cobol_SCL_Proc_Filter.ProgramTypeID IN (13,19)
    LEFT JOIN SCLProcedureSynonyms AS Cobol_SCL_Proc_Alias_Filter ON Programs.ProgramName=Cobol_SCL_Proc_Alias_Filter.SCLProcedureSynonymName
    
    LEFT JOIN SCLStandardProcedures ON DDCLValue.Value=SCLStandardProcedures.Name
WHERE (DDCLProperty.Name = 'TASK')
    AND (DDCLElementParent.DdsTypeId IN (26))
 	AND (DDCLElementParent.Id IN (SELECT * FROM #numeric_param_temp))
    AND (@showStandard='true'  or (SCLStandardProcedures.Name IS null))
    AND (Cobol_SCL_Std_Proc_Filter.Name IS NULL)
    AND (Cobol_SCL_Proc_Filter.ProgramTypeID IS NULL)
    AND (Cobol_SCL_Proc_Alias_Filter.SCLProcedureSynonymName IS NULL)
ORDER BY ProgramName 

	