Using: Microsoft SQL Server 7.00 - 7.00.623 (Intel X86)
I'm creating a stored procedure for a report Web page which allows the user to specify different criteria. I need it to be able to dynamically build parts of the WHERE clause. Here's (some of) what I have so far:
CREATE PROC sp_survey_report
@business char(10),
@sub_business char(10),
@engagement_type char(8),
@full_part_time_ind char(3),
@comp_type char(3)
AS
IF @business = "ALL" SELECT @business = "%"
IF @sub_business = "ALL" SELECT @sub_business = "%"
IF @engagement_type = "ALL" SELECT @engagement_type = "%"
IF @full_part_time_ind = "ALL" SELECT @full_part_time_ind = "%"
IF @comp_type = "ALL" SELECT @comp_type = "%"
SELECT COUNT(*)
FROM t_survey, t_survey_data
WHERE t_survey.survey_id = t_survey_data.survey_id
AND question_id = "1" AND response_value = "01"
AND business like @business
AND sub_business like @sub_business
AND engagement_type like @engagement_type
AND full_part_time_ind like SUBSTRING(@full_part_time_ind, 1, 1)
AND comp_type like SUBSTRING(@comp_type, 1, 1)
...;
('full_part_time_ind' and 'comp_type' are 1-character columns in t_survey)
When I call the SP like this
exec sp_survey_report "SUPPLY", "", "NHR", "F", "E";
I get the correct counts, but when I call the SP like this
exec sp_survey_report "ALL", "ALL", "ALL", "ALL", "ALL";
I get incorrect results. What gives?
Alternatively, I think the following is possible:
CREATE PROCEDURE myProc
blah
blah
AS
DECLARE @command varchar(255)
SELECT @command = "SELECT COUNT(*) FROM blah blah..."
IF some_condition @command = @command + @something
EXEC @command
;
The question is, how can I get the COUNT returned from the executed SELECT statement??
I'm creating a stored procedure for a report Web page which allows the user to specify different criteria. I need it to be able to dynamically build parts of the WHERE clause. Here's (some of) what I have so far:
CREATE PROC sp_survey_report
@business char(10),
@sub_business char(10),
@engagement_type char(8),
@full_part_time_ind char(3),
@comp_type char(3)
AS
IF @business = "ALL" SELECT @business = "%"
IF @sub_business = "ALL" SELECT @sub_business = "%"
IF @engagement_type = "ALL" SELECT @engagement_type = "%"
IF @full_part_time_ind = "ALL" SELECT @full_part_time_ind = "%"
IF @comp_type = "ALL" SELECT @comp_type = "%"
SELECT COUNT(*)
FROM t_survey, t_survey_data
WHERE t_survey.survey_id = t_survey_data.survey_id
AND question_id = "1" AND response_value = "01"
AND business like @business
AND sub_business like @sub_business
AND engagement_type like @engagement_type
AND full_part_time_ind like SUBSTRING(@full_part_time_ind, 1, 1)
AND comp_type like SUBSTRING(@comp_type, 1, 1)
...;
('full_part_time_ind' and 'comp_type' are 1-character columns in t_survey)
When I call the SP like this
exec sp_survey_report "SUPPLY", "", "NHR", "F", "E";
I get the correct counts, but when I call the SP like this
exec sp_survey_report "ALL", "ALL", "ALL", "ALL", "ALL";
I get incorrect results. What gives?
Alternatively, I think the following is possible:
CREATE PROCEDURE myProc
blah
blah
AS
DECLARE @command varchar(255)
SELECT @command = "SELECT COUNT(*) FROM blah blah..."
IF some_condition @command = @command + @something
EXEC @command
;
The question is, how can I get the COUNT returned from the executed SELECT statement??