I have a stored procedure that unions four select statements together. It works as expected. The problem is that I want to also be able to do a dynamic order by using the case statement. When I check the syntax on the stored procedure it get the following error: ERROR 104: ORDER BY items must appear in the select list if the statement contains a UNION operator. My web app passes the parameter sortfield to the query. Any and all suggestions greatly appreciated. Thanks.
CREATE PROCEDURE CRITERIA_DATABIND_SORT
(
@sortfield varchar(50)
)
AS
SELECT c.audit_report_criteria_id, c.region_id, c.capability_id, c.calculation_name, c.calculation_name_alias,
c.calculation_high_bound, c.calculation_low_bound, c.sort_order, c.active_flag, r.region_name AS region_name,
(p.capability_name + ', ' + p.capability_type) AS capability_name, c.tower_id, t.tower_name AS tower_name
FROM AFT_AUDIT_REPORT_CRITERIA c, REGION r, CAPABILITY p, TOWER t
WHERE c.region_id = r.region_id AND c.capability_id = p.capability_id AND c.tower_id = t.tower_id AND c.active_flag = 'y'
UNION ALL
SELECT c.audit_report_criteria_id, c.region_id, c.capability_id, c.calculation_name, c.calculation_name_alias,
c.calculation_high_bound, c.calculation_low_bound, c.sort_order, c.active_flag, r.region_name AS region_name,
('--- None ---') AS capability_name, c.tower_id, t.tower_name AS tower_name
FROM AFT_AUDIT_REPORT_CRITERIA c, REGION r, TOWER t
WHERE c.region_id = r.region_id AND c.capability_id = 0 AND c.tower_id = t.tower_id AND c.active_flag = 'y'
UNION ALL
SELECT c.audit_report_criteria_id, c.region_id, c.capability_id, c.calculation_name, c.calculation_name_alias,
c.calculation_high_bound, c.calculation_low_bound, c.sort_order, c.active_flag, r.region_name AS region_name,
(p.capability_name + ', ' + p.capability_type) AS capability_name, c.tower_id, ('--- None ---') AS tower_name
FROM AFT_AUDIT_REPORT_CRITERIA c, REGION r, CAPABILITY p
WHERE c.region_id = r.region_id AND c.capability_id = p.capability_id AND c.tower_id = 0 AND c.active_flag = 'y'
UNION ALL
SELECT c.audit_report_criteria_id, c.region_id, c.capability_id, c.calculation_name, c.calculation_name_alias,
c.calculation_high_bound, c.calculation_low_bound, c.sort_order, c.active_flag, r.region_name AS region_name,
('--- None ---') AS capability_name, c.tower_id, ('--- None ---') AS tower_name
FROM AFT_AUDIT_REPORT_CRITERIA c, REGION r
WHERE c.region_id = r.region_id AND c.capability_id = 0 AND c.tower_id = 0 AND c.active_flag = 'y'
ORDER BY
CASE WHEN @sortfield='calculation_name' THEN calculation_name
WHEN @sortfield='calculation_name_alias' THEN calculation_name_alias
WHEN @sortfield='region_name' THEN region_name
WHEN @sortfield='capability_name' THEN capability_name
WHEN @sortfield='tower_name' THEN tower_name
WHEN @sortfield='calculation_high_bound' THEN CONVERT(varchar(50), calculation_high_bound)
WHEN @sortfield='calculation_low_bound' THEN CONVERT(varchar(50), calculation_low_bound)
WHEN @sortfield='sort_order' THEN CONVERT(varchar(50), sort_order)
END
GO
Rip Henry
Lewisberry, PA USA
CREATE PROCEDURE CRITERIA_DATABIND_SORT
(
@sortfield varchar(50)
)
AS
SELECT c.audit_report_criteria_id, c.region_id, c.capability_id, c.calculation_name, c.calculation_name_alias,
c.calculation_high_bound, c.calculation_low_bound, c.sort_order, c.active_flag, r.region_name AS region_name,
(p.capability_name + ', ' + p.capability_type) AS capability_name, c.tower_id, t.tower_name AS tower_name
FROM AFT_AUDIT_REPORT_CRITERIA c, REGION r, CAPABILITY p, TOWER t
WHERE c.region_id = r.region_id AND c.capability_id = p.capability_id AND c.tower_id = t.tower_id AND c.active_flag = 'y'
UNION ALL
SELECT c.audit_report_criteria_id, c.region_id, c.capability_id, c.calculation_name, c.calculation_name_alias,
c.calculation_high_bound, c.calculation_low_bound, c.sort_order, c.active_flag, r.region_name AS region_name,
('--- None ---') AS capability_name, c.tower_id, t.tower_name AS tower_name
FROM AFT_AUDIT_REPORT_CRITERIA c, REGION r, TOWER t
WHERE c.region_id = r.region_id AND c.capability_id = 0 AND c.tower_id = t.tower_id AND c.active_flag = 'y'
UNION ALL
SELECT c.audit_report_criteria_id, c.region_id, c.capability_id, c.calculation_name, c.calculation_name_alias,
c.calculation_high_bound, c.calculation_low_bound, c.sort_order, c.active_flag, r.region_name AS region_name,
(p.capability_name + ', ' + p.capability_type) AS capability_name, c.tower_id, ('--- None ---') AS tower_name
FROM AFT_AUDIT_REPORT_CRITERIA c, REGION r, CAPABILITY p
WHERE c.region_id = r.region_id AND c.capability_id = p.capability_id AND c.tower_id = 0 AND c.active_flag = 'y'
UNION ALL
SELECT c.audit_report_criteria_id, c.region_id, c.capability_id, c.calculation_name, c.calculation_name_alias,
c.calculation_high_bound, c.calculation_low_bound, c.sort_order, c.active_flag, r.region_name AS region_name,
('--- None ---') AS capability_name, c.tower_id, ('--- None ---') AS tower_name
FROM AFT_AUDIT_REPORT_CRITERIA c, REGION r
WHERE c.region_id = r.region_id AND c.capability_id = 0 AND c.tower_id = 0 AND c.active_flag = 'y'
ORDER BY
CASE WHEN @sortfield='calculation_name' THEN calculation_name
WHEN @sortfield='calculation_name_alias' THEN calculation_name_alias
WHEN @sortfield='region_name' THEN region_name
WHEN @sortfield='capability_name' THEN capability_name
WHEN @sortfield='tower_name' THEN tower_name
WHEN @sortfield='calculation_high_bound' THEN CONVERT(varchar(50), calculation_high_bound)
WHEN @sortfield='calculation_low_bound' THEN CONVERT(varchar(50), calculation_low_bound)
WHEN @sortfield='sort_order' THEN CONVERT(varchar(50), sort_order)
END
GO
Rip Henry
Lewisberry, PA USA