DYNAMIC RESULT SETS 1
LANGUAGE SQL
ss: BEGIN
            

	            BEGIN
					    DECLARE crs CURSOR WITH RETURN FOR
					    SELECT  *
					    FROM
					         (
						 SELECT DISTINCT
					            Variables_1.VarID,
					            StatementReference_1.bRead AS VariableReferenceType,
					            StatementReference_2.ResourceType,
					            StatementReference_2.ResourceID,
					            StatementReference_2.bRead AS ResourceReferenceType,
					            st.Description AS StatementType,
					            os.StartRow,
					            pth.PathStr,
					            pa.AliasName AS ProgramName,
					            pt.ProgramType,
					            prg.ProgramID,
					            os.OccurID
					     FROM StatementReference AS StatementReference_1
					               INNER JOIN Variables AS Variables_1 ON StatementReference_1.ResourceID = Variables_1.VarID
					               INNER JOIN StatementReference AS StatementReference_2 ON StatementReference_1.OccurID = StatementReference_2.OccurID
					                                                                            AND StatementReference_1.bRead <> StatementReference_2.bRead
					               INNER JOIN Statements st ON StatementReference_1.StatementType = st.StatementType
					               INNER JOIN OccurrencesStmt os ON StatementReference_1.OccurID = os.OccurID
					               INNER JOIN Paths pth ON os.PathID = pth.PathID
					               INNER JOIN Programs prg ON os.ProgID = prg.ProgramID
								   INNER JOIN ProgramAliases pa ON pa.ProgramID = prg.ProgramID AND pa.AliasType = 0
					               INNER JOIN ProgramTypes pt ON prg.ProgramTypeID = pt.ProgramTypeID
					     WHERE StatementReference_1.ResourceType = 4
					           AND StatementReference_2.ResourceType IN (1, 6, 9, 18, 19, 20, 21, 56, 61, 62, 69, 70, 77, 78, 81, 82, 182, 189, 190, 193, 214)
					           AND Variables_1.VarID IN (SELECT * FROM SESSION.numeric_param_temp)
					           AND StatementReference_1.bRead = 2
					           AND StatementReference_2.bRead = 1
					           
					     UNION
					     
					     SELECT DISTINCT Variables_1.VarID, 
								2 AS VariableReferenceType, 
								1000 as ResourceType, 
								Variables_1.VarID  as ResourceID, 
								1 AS ResourceReferenceType, 
								'Initial Value' AS StatementType, 
								Occurrences.StartRow, 
								Paths.PathStr, 
								ProgramAliases.AliasName AS ProgramName, 
								ProgramTypes.ProgramType,
								Programs.ProgramID,
								Occurrences.OccurID
							FROM 
								 Variables AS Variables_1
								INNER JOIN Occurrences on Occurrences.OccurID = Variables_1.OccurID
								INNER JOIN Paths ON Occurrences.PathID = Paths.PathID 
								INNER JOIN Programs ON Occurrences.ProgID = Programs.ProgramID
								INNER JOIN ProgramAliases ON ProgramAliases.ProgramId = Programs.ProgramID and ProgramAliases.AliasType = 0 
								INNER JOIN ProgramTypes ON Programs.ProgramTypeID = ProgramTypes.ProgramTypeID
							WHERE 
								szValues <> 'NULL'
								AND (Variables_1.VarID IN (SELECT * FROM SESSION.numeric_param_temp))      
					      )src
					     ORDER BY src.VarID;
						      
				        OPEN crs;
				
                END;
END ss	



