AS
set NOCOUNT ON;
IF OBJECT_ID('tempdb..#programWithAlias') IS NOT NULL DROP TABLE #programWithAlias
			 SELECT Programs.Ancestor, Programs.OccurID, Programs.ProgramID, Programs.ProgramTypeID,
				pa.AliasName AS ProgramName
			 INTO #programWithAlias
			 FROM  dbo.Programs INNER JOIN dbo.ProgramAliases pa  ON Programs.ProgramID = pa.ProgramId and pa.AliasType = 0

SELECT Programs_1.ProgramName AS l, pa.ProgramName as k, StatementReference.StartRow as m, Programs_1.ProgramTypeID as n
FROM #programWithAlias pa
INNER JOIN OccurrencesStmt ON pa.ProgramID = OccurrencesStmt.ProgID INNER JOIN StatementReference ON OccurrencesStmt.OccurID = StatementReference.OccurID
INNER JOIN #programWithAlias AS Programs_1 ON StatementReference.ResourceID = Programs_1.ProgramID
INNER JOIN Occurrences on Occurrences.OccurID=pa.OccurID INNER JOIN Paths on Occurrences.PathID=Paths.PathID
WHERE (StatementReference.ResourceType = 5)
AND Programs_1.OccurID=0
AND pa.OccurID <> 0
AND (pa.Ancestor is null OR pa.Ancestor='' OR pa.ProgramTypeID=19 )
AND (pa.ProgramID in (Select * from #numeric_param_temp))
AND ( ( Programs_1.ProgramTypeID in (19)
		AND (Programs_1.ProgramName not in (select * from #missing_string_param_temp) )
	  	AND Programs_1.ProgramName not like 'ICLSTDSW.%' AND Programs_1.ProgramName not like 'ICL9%' AND Programs_1.ProgramName not like 'ICLCTM.%'
	  )
	OR (Programs_1.ProgramTypeID not in (-1,13,19))
	OR ( Programs_1.ProgramTypeid=-1
	 		and StatementReference.StartRow not in (SELECT DISTINCT StartRow FROM #calling_temp WHERE ProgramName = pa.ProgramName AND Ancestor = pa.Ancestor AND PathStr=Paths.PathStr)
	    )
	)
AND (Programs_1.ProgramName not in (SELECT ProgramName from EZViewer_DDCL_Callable_View ))
UNION
SELECT Programs_1.ProgramName AS l, pa.Ancestor as k, StatementReference.StartRow as m, Programs_1.ProgramTypeID as n
FROM #programWithAlias pa
INNER JOIN OccurrencesStmt ON pa.ProgramID = OccurrencesStmt.ProgID INNER JOIN StatementReference ON OccurrencesStmt.OccurID = StatementReference.OccurID
INNER JOIN #programWithAlias AS Programs_1 ON StatementReference.ResourceID = Programs_1.ProgramID
INNER JOIN Occurrences on Occurrences.OccurID=pa.OccurID INNER JOIN Paths on Occurrences.PathID=Paths.PathID
WHERE (StatementReference.ResourceType = 5)
AND Programs_1.OccurID=0
AND pa.OccurID <> 0
AND pa.Ancestor is not null AND pa.Ancestor <> '' AND pa.ProgramTypeID in (15,16)
AND (pa.ProgramID in ( select DISTINCT p1.programid from programs as p1 , ProgramAliases as p2 where p1.ancestor=p2.AliasName and p2.ProgramId in (Select * from #numeric_param_temp)))
AND ( ( Programs_1.ProgramTypeID in (19)
		AND (Programs_1.ProgramName not in (select * from #missing_string_param_temp) )
	  	AND Programs_1.ProgramName not like 'ICLSTDSW.%' AND Programs_1.ProgramName not like 'ICL9%' AND Programs_1.ProgramName not like 'ICLCTM.%'
	  )
	OR (Programs_1.ProgramTypeID not in (-1,13,19))
	OR ( Programs_1.ProgramTypeid=-1
	 		and StatementReference.StartRow not in (SELECT DISTINCT StartRow FROM #calling_temp WHERE ProgramName = pa.ProgramName AND Ancestor = pa.Ancestor AND PathStr=Paths.PathStr)
	    )
	)
AND (Programs_1.ProgramName not in (SELECT ProgramName from EZViewer_DDCL_Callable_View ))
ORDER BY n, l, k, m
