DYNAMIC RESULT SETS 1
LANGUAGE SQL
 BEGIN

	            BEGIN
					                 DECLARE crs CURSOR WITH RETURN FOR
					                 SELECT dde.Name
								     FROM DDCLElement dde
								     WHERE dde.DdsTypeId = 14
									       AND dde.Id IN (SELECT * FROM SESSION.numeric_param_temp)
								           AND dde.id NOT IN
													    (
														   SELECT DISTINCT
																dde1.id
														   FROM DDCLElement dde1
															   RIGHT JOIN DDCLValue ddv ON dde1.Name = ddv.ReferenceName
																					 AND dde1.DdsTypeId = ddv.ReferenceDdsTypeId
															   INNER JOIN DDCLProperty ddp ON ddv.PropertyId = ddp.Id
															   INNER JOIN DDCLElement AS DDCLElementParent ON ddv.ElementId = DDCLElementParent.Id
														   WHERE ddv.referenceddstypeid = 14
															    AND dde1.islocal = 0
															    AND ddv.ValueTypeId IN (2, 4)
															    AND (
															         (DDCLElementParent.DdsTypeId IN (11, 16, 26) AND ddp.Name = 'DISPLAY-STRUCTURE')
														               OR
																    (DDCLElementParent.DdsTypeId = 11 AND ddp.Name = 'PRINT-FORM')
																    )
													    )
								     AND dde.id NOT IN
													    (
														   SELECT DISTINCT
																  DDCLElementChild.Id AS ChildId
														   FROM DDCLElement dde2
															   INNER JOIN DDCLValue AS DDCLValue_1 ON dde2.Id = DDCLValue_1.ElementId
															   INNER JOIN DDCLProperty AS DDCLProperty_1 ON DDCLValue_1.PropertyId = DDCLProperty_1.Id
															   INNER JOIN DDCLValue AS DDCLValue_2 ON DDCLValue_1.ReferenceName = 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
														   WHERE dde2.DdsTypeId IN (11, 16)
															   AND DDCLValue_1.ReferenceDdsTypeId = 15
															   AND DDCLProperty_1.Name = 'DISPLAY-STRUCTURE'
															   AND DDCLValue_2.ReferenceDdsTypeId = 15
															   AND DDCLProperty_2.Name = 'DISPLAY-STRUCTURE'
															   AND DDCLElementChild.DdsTypeId = 14
															   AND DDCLElementChild.Id IN (SELECT * FROM SESSION.numeric_param_temp)
														
													    )
								     AND dde.id NOT IN
													(
								
														  SELECT DISTINCT
															    DDCLElementChild.Id
														  FROM DDCLElement dde3
															  INNER JOIN DDCLValue AS DDCLValue_1 ON dde3.Id = DDCLValue_1.ElementId
															  INNER JOIN DDCLProperty AS DDCLProperty_1 ON DDCLValue_1.PropertyId = DDCLProperty_1.Id
															  INNER JOIN DDCLElement AS DDCLElement_BLOCK ON DDCLValue_1.ReferenceName = DDCLElement_BLOCK.Name
																									   AND DDCLValue_1.ReferenceDdsTypeId = DDCLElement_BLOCK.DdsTypeId
															  INNER JOIN DDCLValue AS DDCLValue_2 ON DDCLElement_BLOCK.Name = 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
														  WHERE dde3.DdsTypeId IN (11)
															  AND DDCLValue_1.ReferenceDdsTypeId = 15
															  AND DDCLProperty_1.Name = 'PRINT-FORM'
															  AND DDCLProperty_2.Name = 'DISPLAY-STRUCTURE'
															  AND DDCLElementChild.DdsTypeId = 14
															  AND DDCLElementChild.Id IN (SELECT * FROM SESSION.numeric_param_temp)
																				
													)
								     ORDER BY dde.Name;	
								
				                     OPEN crs;
				
                END;
END	
