AS set NOCOUNT ON;
IF OBJECT_ID('tempdb..#programWithAncestor') IS NOT NULL DROP TABLE #programWithAncestor
			 SELECT *
			 INTO #programWithAncestor 
			 FROM   fGetProgram_Ancestor_Aliases (NULL)
			 WHERE ProgramID in (Select * from #numeric_param_temp)

SELECT pa.ProgramName, 
 SqlTables.TableName, 
 Statements.Description,
 Count(DISTINCT OccurrencesStmt.OccurID) AS stmtCount, pa.AncestorName, pa.ProgramID, pa.AncestorID 
FROM (
          (
             (
              StatementReference INNER JOIN OccurrencesStmt ON StatementReference.OccurID = OccurrencesStmt.OccurID
             ) INNER JOIN Statements ON OccurrencesStmt.StatementType = Statements.StatementType
          ) INNER JOIN #programWithAncestor pa ON OccurrencesStmt.ProgID = pa.ProgramID
        ) INNER JOIN SqlTables ON StatementReference.ResourceID = SqlTables.SqlTableID
WHERE (
StatementReference.ResourceType=1)
GROUP BY pa.AncestorName, pa.ProgramName, SqlTables.TableName, Statements.Description, pa.ProgramID, pa.AncestorID
ORDER BY pa.AncestorName, pa.ProgramName, pa.ProgramID, SqlTables.TableName 