DYNAMIC RESULT SETS 1
LANGUAGE SQL
ss: BEGIN
           

	            BEGIN
					   DECLARE crs CURSOR WITH RETURN FOR
					    SELECT *
						FROM (
							  SELECT 
								    dde.Id,
								    dde.Name,
								    dde.DdsTypeId,
								    DDCLElementChild.Id AS ChildId,
								    ElementSourceInfo.StartRow AS ElementStartRow,
								    ElementSourceInfo.EndRow AS ElementEndRow,
								    Paths_1.PathStr AS ElementPathStr,
								    CallSourceInfo.StartRow AS CallStartRow,
								    CallSourceInfo.EndRow AS CallEndRow,
								    Paths_2.PathStr AS CallPathStr
							  FROM DDCLElement dde
								  INNER JOIN DDCLValue ddv ON dde.Id = ddv.ElementId
								  INNER JOIN DDCLProperty ddp ON ddv.PropertyId = ddp.Id
								  INNER JOIN DDCLElement AS DDCLElementChild ON UPPER(ddv.ReferenceName) = UPPER(DDCLElementChild.Name)
																		  AND ddv.ReferenceDdsTypeId = DDCLElementChild.DdsTypeId
								  INNER JOIN DDCLSourceInfo AS ElementSourceInfo ON dde.SourceInfo = ElementSourceInfo.Id
								  INNER JOIN Programs AS Programs_1 ON ElementSourceInfo.ProgramId = Programs_1.ProgramID
								  INNER JOIN Occurrences AS Occurrences_1 ON Programs_1.OccurID = Occurrences_1.OccurID
								  INNER JOIN Paths AS Paths_1 ON Occurrences_1.PathID = Paths_1.PathID
								  INNER JOIN DDCLSourceInfo AS CallSourceInfo ON ddv.SourceInfo = CallSourceInfo.Id
								  INNER JOIN Programs AS Programs_2 ON CallSourceInfo.ProgramId = Programs_2.ProgramID
								  INNER JOIN Occurrences AS Occurrences_2 ON Programs_2.OccurID = Occurrences_2.OccurID
								  INNER JOIN Paths AS Paths_2 ON Occurrences_2.PathID = Paths_2.PathID
							  WHERE
							       DDCLElementChild.Id IN (SELECT * FROM SESSION.numeric_param_temp) AND 
								   ddv.ReferenceDdsTypeId = 14
								  AND (
									    (dde.DdsTypeId = 11  AND UPPER(ddp.Name) = 'PRINT-FORM')
										OR 
									    (dde.DdsTypeId IN (11, 16, 26) AND UPPER(ddp.Name) = 'DISPLAY-STRUCTURE')
									  )
				    
				                   UNION
				     
								SELECT 
									  dde.Id,
									  dde.Name,
									  dde.DdsTypeId,
									  DDCLElementChild.Id AS ChildId,
									  ElementSourceInfo.StartRow AS ElementStartRow,
									  ElementSourceInfo.EndRow AS ElementEndRow,
									  Paths_1.PathStr AS ElementPathStr,
									  CallSourceInfo.StartRow AS CallStartRow,
									  CallSourceInfo.EndRow AS CallEndRow,
									  Paths_2.PathStr AS CallPathStr
								FROM DDCLElement dde
									INNER JOIN DDCLValue AS DDCLValue_1 ON dde.Id = DDCLValue_1.ElementId
									INNER JOIN DDCLProperty AS DDCLProperty_1 ON DDCLValue_1.PropertyId = DDCLProperty_1.Id
									INNER JOIN DDCLValue AS DDCLValue_2 ON UPPER(DDCLValue_1.ReferenceName) = UPPER(DDCLValue_2.ReferenceName)
																	   AND DDCLValue_1.ReferenceDdsTypeId = DDCLValue_2.ReferenceDdsTypeId
									INNER JOIN DDCLProperty AS DDCLProperty_2 ON DDCLValue_2.PropertyId = DDCLProperty_2.Id
									INNER JOIN DDCLElement AS DDCLElementChild ON DDCLValue_2.ElementId = DDCLElementChild.Id
									INNER JOIN DDCLSourceInfo AS ElementSourceInfo ON dde.SourceInfo = ElementSourceInfo.Id
									INNER JOIN Programs AS Programs_1 ON ElementSourceInfo.ProgramId = Programs_1.ProgramID
									INNER JOIN Occurrences AS Occurrences_1 ON Programs_1.OccurID = Occurrences_1.OccurID
									INNER JOIN Paths AS Paths_1 ON Occurrences_1.PathID = Paths_1.PathID
									INNER JOIN DDCLSourceInfo AS CallSourceInfo ON DDCLValue_2.SourceInfo = CallSourceInfo.Id
									INNER JOIN Programs AS Programs_2 ON CallSourceInfo.ProgramId = Programs_2.ProgramID
									INNER JOIN Occurrences AS Occurrences_2 ON Programs_2.OccurID = Occurrences_2.OccurID
									INNER JOIN Paths AS Paths_2 ON Occurrences_2.PathID = Paths_2.PathID
								WHERE dde.DdsTypeId IN (11, 16)
									AND DDCLValue_1.ReferenceDdsTypeId = 15
									AND DDCLValue_2.ReferenceDdsTypeId = 15
									AND DDCLElementChild.DdsTypeId = 14
									AND UPPER(DDCLProperty_1.Name) = 'DISPLAY-STRUCTURE'
									AND UPPER(DDCLProperty_2.Name) = 'DISPLAY-STRUCTURE'
									AND DDCLElementChild.Id IN (SELECT * FROM SESSION.numeric_param_temp)
				
				
				                UNION
				     
							 SELECT dde.Id,
								   dde.Name,
								   dde.DdsTypeId,
								   DDCLElementChild.Id AS ChildId,
								   ElementSourceInfo.StartRow AS ElementStartRow,
								   ElementSourceInfo.EndRow AS ElementEndRow,
								   Paths_1.PathStr AS ElementPathStr,
								   CallSourceInfo.StartRow AS CallStartRow,
								   CallSourceInfo.EndRow AS CallEndRow,
								   Paths_2.PathStr AS CallPathStr
							 FROM DDCLElement dde
								 INNER JOIN DDCLValue AS DDCLValue_1 ON dde.Id = DDCLValue_1.ElementId
								 INNER JOIN DDCLProperty AS DDCLProperty_1 ON DDCLValue_1.PropertyId = DDCLProperty_1.Id
								 INNER JOIN DDCLElement AS DDCLElement_BLOCK ON UPPER(DDCLValue_1.ReferenceName) = UPPER(DDCLElement_BLOCK.Name)
																		  AND DDCLValue_1.ReferenceDdsTypeId = DDCLElement_BLOCK.DdsTypeId
								 INNER JOIN DDCLValue AS DDCLValue_2 ON UPPER(DDCLElement_BLOCK.Name) = UPPER(DDCLValue_2.ReferenceName)
																    AND DDCLElement_BLOCK.DdsTypeId = DDCLValue_2.ReferenceDdsTypeId
								 INNER JOIN DDCLProperty AS DDCLProperty_2 ON DDCLValue_2.PropertyId = DDCLProperty_2.Id
								 INNER JOIN DDCLElement AS DDCLElementChild ON DDCLValue_2.ElementId = DDCLElementChild.Id
								 INNER JOIN DDCLSourceInfo AS ElementSourceInfo ON dde.SourceInfo = ElementSourceInfo.Id
								 INNER JOIN Programs AS Programs_1 ON ElementSourceInfo.ProgramId = Programs_1.ProgramID
								 INNER JOIN Occurrences AS Occurrences_1 ON Programs_1.OccurID = Occurrences_1.OccurID
								 INNER JOIN Paths AS Paths_1 ON Occurrences_1.PathID = Paths_1.PathID
								 INNER JOIN DDCLSourceInfo AS CallSourceInfo ON DDCLValue_2.SourceInfo = CallSourceInfo.Id
								 INNER JOIN Programs AS Programs_2 ON CallSourceInfo.ProgramId = Programs_2.ProgramID
								 INNER JOIN Occurrences AS Occurrences_2 ON Programs_2.OccurID = Occurrences_2.OccurID
								 INNER JOIN Paths AS Paths_2 ON Occurrences_2.PathID = Paths_2.PathID
							 WHERE dde.DdsTypeId = 11
								 AND DDCLValue_1.ReferenceDdsTypeId = 15
								 AND DDCLElementChild.DdsTypeId = 14
								 AND UPPER(DDCLProperty_1.Name) = 'PRINT-FORM'
								 AND UPPER(DDCLProperty_2.Name) = 'DISPLAY-STRUCTURE'
								 AND DDCLElementChild.Id IN (SELECT * FROM SESSION.numeric_param_temp) 
				                ) src
							 ORDER BY src.Name;

				     OPEN crs;
				
                END;
END ss 