Hi:
How can I insert the results of a query (generated via a stored procedure) into a temporary table and repeatedly reuse the results inserted to perform some aggregate calculations?
I have a stored procedure that takes several views that I created from one table. Using CASE statements within the stored procedure (sp_CustSurveyData), I take each view and generate what is the equivalent to a cross tab (like in MS Access) for each view. Then I take each of the crosstab results and merge them into one result set using the UNION statement.
The stored procedure is as follows:
CREATE PROCEDURE sp_CustSurveyData AS
SELECT record, datesubmitted, 'Appearance' As Category,
CASE Appearance
WHEN 'OUTSTANDING' THEN 1 ELSE 0 END AS OUTSTANDING,
CASE Appearance
WHEN 'EXCELLENT' THEN 1 ELSE 0 END AS EXCELLENT,
CASE Appearance
WHEN 'GOOD' THEN 1 ELSE 0 END AS GOOD,
CASE Appearance
WHEN 'AVERAGE' THEN 1 ELSE 0 END AS AVERAGE,
CASE Appearance
WHEN 'POOR' THEN 1 ELSE 0 END AS POOR
FROM vw_custsurv_appearance
Union
SELECT record, datesubmitted, 'Prompt' As Category,
CASE Prompt
WHEN 'OUTSTANDING' THEN 1 ELSE 0 END AS OUTSTANDING,
CASE Prompt
WHEN 'EXCELLENT' THEN 1 ELSE 0 END AS EXCELLENT,
CASE Prompt
WHEN 'GOOD' THEN 1 ELSE 0 END AS GOOD,
CASE Prompt
WHEN 'AVERAGE' THEN 1 ELSE 0 END AS AVERAGE,
CASE Prompt
WHEN 'POOR' THEN 1 ELSE 0 END AS POOR
FROM vw_custsurv_prompt
Union
SELECT record, datesubmitted, 'Courteous' As Category,
CASE Courteous
WHEN 'OUTSTANDING' THEN 1 ELSE 0 END AS OUTSTANDING,
CASE Courteous
WHEN 'EXCELLENT' THEN 1 ELSE 0 END AS EXCELLENT,
CASE Courteous
WHEN 'GOOD' THEN 1 ELSE 0 END AS GOOD,
CASE Courteous
WHEN 'AVERAGE' THEN 1 ELSE 0 END AS AVERAGE,
CASE Courteous
WHEN 'POOR' THEN 1 ELSE 0 END AS POOR
FROM vw_custsurv_courteous
Union
SELECT record, datesubmitted,' professionalism' As Category,
CASE Professionalism
WHEN 'OUTSTANDING' THEN 1 ELSE 0 END AS OUTSTANDING,
CASE Professionalism
WHEN 'EXCELLENT' THEN 1 ELSE 0 END AS EXCELLENT,
CASE Professionalism
WHEN 'GOOD' THEN 1 ELSE 0 END AS GOOD,
CASE Professionalism
WHEN 'AVERAGE' THEN 1 ELSE 0 END AS AVERAGE,
CASE Professionalism
WHEN 'POOR' THEN 1 ELSE 0 END AS POOR
FROM vw_custsurv_professionalism
Union
SELECT record, datesubmitted, 'MetExpectations' As Category,
CASE MetExpectations
WHEN 'OUTSTANDING' THEN 1 ELSE 0 END AS OUTSTANDING,
CASE MetExpectations
WHEN 'EXCELLENT' THEN 1 ELSE 0 END AS EXCELLENT,
CASE MetExpectations
WHEN 'GOOD' THEN 1 ELSE 0 END AS GOOD,
CASE MetExpectations
WHEN 'AVERAGE' THEN 1 ELSE 0 END AS AVERAGE,
CASE MetExpectations
WHEN 'POOR' THEN 1 ELSE 0 END AS POOR
FROM vw_custsurv_MetExpectation
Union
SELECT record, datesubmitted, 'overallrating' As Category,
CASE overallrating
WHEN 'OUTSTANDING' THEN 1 ELSE 0 END AS OUTSTANDING,
CASE overallrating
WHEN 'EXCELLENT' THEN 1 ELSE 0 END AS EXCELLENT,
CASE overallrating
WHEN 'GOOD' THEN 1 ELSE 0 END AS GOOD,
CASE overallrating
WHEN 'AVERAGE' THEN 1 ELSE 0 END AS AVERAGE,
CASE overallrating
WHEN 'POOR' THEN 1 ELSE 0 END AS POOR
FROM vw_custsurv_overall
Union
SELECT record, datesubmitted, 'quality' As Category,
CASE quality
WHEN 'OUTSTANDING' THEN 1 ELSE 0 END AS OUTSTANDING,
CASE quality
WHEN 'EXCELLENT' THEN 1 ELSE 0 END AS EXCELLENT,
CASE quality
WHEN 'GOOD' THEN 1 ELSE 0 END AS GOOD,
CASE quality
WHEN 'AVERAGE' THEN 1 ELSE 0 END AS AVERAGE,
CASE quality
WHEN 'POOR' THEN 1 ELSE 0 END AS POOR
FROM vw_custsurv_quality
After the stored procedure is executed, the result set looks like the following:
record datesubmitted Category OUTSTANDING EXCELLENT GOOD AVERAGE POOR
-------- -------------- -------------- ---------------- ----------- ----------- ----------- ----------- -----------
3 Sep 25 2002 2:43PM professionalism 0 0 0 1 0
3 Sep 25 2002 2:43PM Appearance 0 0 0 0 1
3 Sep 25 2002 2:43PM Courteous 0 0 0 1 0
3 Sep 25 2002 2:43PM MetExpectations 0 0 0 1 0
3 Sep 25 2002 2:43PM overallrating 0 0 0 1 0
3 Sep 25 2002 2:43PM Prompt 0 0 0 1 0
3 Sep 25 2002 2:43PM quality 0 1 0 0 0
4 Sep 25 2002 3:57PM professionalism 0 1 0 0 0
4 Sep 25 2002 3:57PM Appearance 0 0 0 1 0
4 Sep 25 2002 3:57PM Courteous 0 1 0 0 0
4 Sep 25 2002 3:57PM MetExpectations 0 0 0 1 0
4 Sep 25 2002 3:57PM overallrating 0 1 0 0 0
4 Sep 25 2002 3:57PM Prompt 1 0 0 0 0
4 Sep 25 2002 3:57PM quality 0 0 0 1 0
5 Sep 30 2002 8:11AM professionalism 0 0 0 1 0
5 Sep 30 2002 8:11AM Appearance 0 1 0 0 0
5 Sep 30 2002 8:11AM Courteous 0 0 0 1 0
5 Sep 30 2002 8:11AM MetExpectations 0 0 0 0 1
5 Sep 30 2002 8:11AM overallrating 0 0 0 1 0
5 Sep 30 2002 8:11AM Prompt 0 1 0 0 0
5 Sep 30 2002 8:11AM quality 0 0 0 1 0
6 Oct 16 2002 11:58AM professionalism 0 1 0 0 0
6 Oct 16 2002 11:58AM Appearance 0 0 0 1 0
6 Oct 16 2002 11:58AM Courteous 0 1 0 0 0
6 Oct 16 2002 11:58AM MetExpectations 0 1 0 0 0
6 Oct 16 2002 11:58AM overallrating 0 1 0 0 0
6 Oct 16 2002 11:58AM Prompt 0 1 0 0 0
6 Oct 16 2002 11:58AM quality 0 1 0 0 0
7 Oct 17 2002 7:36AM professionalism 0 1 0 0 0
7 Oct 17 2002 7:36AM Appearance 0 0 0 1 0
7 Oct 17 2002 7:36AM Courteous 0 1 0 0 0
7 Oct 17 2002 7:36AM MetExpectations 0 1 0 0 0
7 Oct 17 2002 7:36AM overallrating 0 1 0 0 0
7 Oct 17 2002 7:36AM Prompt 0 1 0 0 0
7 Oct 17 2002 7:36AM quality 0 1 0 0 0
(35 row(s) affected)
I am trying to generate some totals (sums) for each of the combinations of category (i.e. Appearance, Professionalism) and OUTSTANDING, EXCELLENT, GOOD, AVERAGE, POOR. I want to group the categories and them sum each of the columns (1s).
To do this, I want to run the following SQL statement:
SELECT category, Sum(OUTSTANDING) AS SumOfOUTSTANDING, Sum(EXCELLENT) AS SumOfEXCELLENT, Sum(GOOD) AS SumOfGOOD, Sum(AVERAGE) AS SumOfAVERAGE, Sum(POOR) AS SumOfPOOR
FROM #t
GROUP BY category
My problem is that I’m not sure how to take the results (35 rows above) generated by the stored procedure and use the SELECT SQL statement to generate a result like shown below:
My final result should look like:
category SumOfOUTSTANDING SumOfEXCELLENT SumOfGOOD SumOfAVERAGE SumOfPOOR
Appearance 0 2 0 2 1
Courteous 1 2 0 2 0
MetExpectations 1 1 0 2 1
OverallRating 1 1 0 3 0
Professionalism 1 2 0 2 0
Prompt 0 4 0 1 0
Quality 0 3 0 2 0
I tried to use another stored procedure to create a temporary table and then insert the result set (35 rows) into the temporary table. Then use the SELECT SQL statement above based on the temporary table to get the final result.
I composed the following stored procedure to insert the 35 row result set:
CREATE PROCEDURE myTempTable
AS
CREATE TABLE #myTempTable (
record numeric (9) NOT NULL,
datesubmitted datetime NOT NULL,
Category nvarchar (15) NULL,
OUTSTANDING numeric (9) NULL,
EXCELLENT numeric (9) NULL,
GOOD numeric (9) NULL,
AVERAGE numeric (9) NULL,
POOR numeric (9) NULL
)
INSERT #myTempTable
exec sp_CustSurveyData ‘original stored procedure that generated the 35 rows.
But instead of getting the final result set, I received the following error message in SQL Server 2000:
Server: Msg 8152, Level 16, State 4, Line 0
String or binary data would be truncated.
(0 row(s) affected)
What I do wrong in the process? Any helpful advice would be appreciated.
Thanks
How can I insert the results of a query (generated via a stored procedure) into a temporary table and repeatedly reuse the results inserted to perform some aggregate calculations?
I have a stored procedure that takes several views that I created from one table. Using CASE statements within the stored procedure (sp_CustSurveyData), I take each view and generate what is the equivalent to a cross tab (like in MS Access) for each view. Then I take each of the crosstab results and merge them into one result set using the UNION statement.
The stored procedure is as follows:
CREATE PROCEDURE sp_CustSurveyData AS
SELECT record, datesubmitted, 'Appearance' As Category,
CASE Appearance
WHEN 'OUTSTANDING' THEN 1 ELSE 0 END AS OUTSTANDING,
CASE Appearance
WHEN 'EXCELLENT' THEN 1 ELSE 0 END AS EXCELLENT,
CASE Appearance
WHEN 'GOOD' THEN 1 ELSE 0 END AS GOOD,
CASE Appearance
WHEN 'AVERAGE' THEN 1 ELSE 0 END AS AVERAGE,
CASE Appearance
WHEN 'POOR' THEN 1 ELSE 0 END AS POOR
FROM vw_custsurv_appearance
Union
SELECT record, datesubmitted, 'Prompt' As Category,
CASE Prompt
WHEN 'OUTSTANDING' THEN 1 ELSE 0 END AS OUTSTANDING,
CASE Prompt
WHEN 'EXCELLENT' THEN 1 ELSE 0 END AS EXCELLENT,
CASE Prompt
WHEN 'GOOD' THEN 1 ELSE 0 END AS GOOD,
CASE Prompt
WHEN 'AVERAGE' THEN 1 ELSE 0 END AS AVERAGE,
CASE Prompt
WHEN 'POOR' THEN 1 ELSE 0 END AS POOR
FROM vw_custsurv_prompt
Union
SELECT record, datesubmitted, 'Courteous' As Category,
CASE Courteous
WHEN 'OUTSTANDING' THEN 1 ELSE 0 END AS OUTSTANDING,
CASE Courteous
WHEN 'EXCELLENT' THEN 1 ELSE 0 END AS EXCELLENT,
CASE Courteous
WHEN 'GOOD' THEN 1 ELSE 0 END AS GOOD,
CASE Courteous
WHEN 'AVERAGE' THEN 1 ELSE 0 END AS AVERAGE,
CASE Courteous
WHEN 'POOR' THEN 1 ELSE 0 END AS POOR
FROM vw_custsurv_courteous
Union
SELECT record, datesubmitted,' professionalism' As Category,
CASE Professionalism
WHEN 'OUTSTANDING' THEN 1 ELSE 0 END AS OUTSTANDING,
CASE Professionalism
WHEN 'EXCELLENT' THEN 1 ELSE 0 END AS EXCELLENT,
CASE Professionalism
WHEN 'GOOD' THEN 1 ELSE 0 END AS GOOD,
CASE Professionalism
WHEN 'AVERAGE' THEN 1 ELSE 0 END AS AVERAGE,
CASE Professionalism
WHEN 'POOR' THEN 1 ELSE 0 END AS POOR
FROM vw_custsurv_professionalism
Union
SELECT record, datesubmitted, 'MetExpectations' As Category,
CASE MetExpectations
WHEN 'OUTSTANDING' THEN 1 ELSE 0 END AS OUTSTANDING,
CASE MetExpectations
WHEN 'EXCELLENT' THEN 1 ELSE 0 END AS EXCELLENT,
CASE MetExpectations
WHEN 'GOOD' THEN 1 ELSE 0 END AS GOOD,
CASE MetExpectations
WHEN 'AVERAGE' THEN 1 ELSE 0 END AS AVERAGE,
CASE MetExpectations
WHEN 'POOR' THEN 1 ELSE 0 END AS POOR
FROM vw_custsurv_MetExpectation
Union
SELECT record, datesubmitted, 'overallrating' As Category,
CASE overallrating
WHEN 'OUTSTANDING' THEN 1 ELSE 0 END AS OUTSTANDING,
CASE overallrating
WHEN 'EXCELLENT' THEN 1 ELSE 0 END AS EXCELLENT,
CASE overallrating
WHEN 'GOOD' THEN 1 ELSE 0 END AS GOOD,
CASE overallrating
WHEN 'AVERAGE' THEN 1 ELSE 0 END AS AVERAGE,
CASE overallrating
WHEN 'POOR' THEN 1 ELSE 0 END AS POOR
FROM vw_custsurv_overall
Union
SELECT record, datesubmitted, 'quality' As Category,
CASE quality
WHEN 'OUTSTANDING' THEN 1 ELSE 0 END AS OUTSTANDING,
CASE quality
WHEN 'EXCELLENT' THEN 1 ELSE 0 END AS EXCELLENT,
CASE quality
WHEN 'GOOD' THEN 1 ELSE 0 END AS GOOD,
CASE quality
WHEN 'AVERAGE' THEN 1 ELSE 0 END AS AVERAGE,
CASE quality
WHEN 'POOR' THEN 1 ELSE 0 END AS POOR
FROM vw_custsurv_quality
After the stored procedure is executed, the result set looks like the following:
record datesubmitted Category OUTSTANDING EXCELLENT GOOD AVERAGE POOR
-------- -------------- -------------- ---------------- ----------- ----------- ----------- ----------- -----------
3 Sep 25 2002 2:43PM professionalism 0 0 0 1 0
3 Sep 25 2002 2:43PM Appearance 0 0 0 0 1
3 Sep 25 2002 2:43PM Courteous 0 0 0 1 0
3 Sep 25 2002 2:43PM MetExpectations 0 0 0 1 0
3 Sep 25 2002 2:43PM overallrating 0 0 0 1 0
3 Sep 25 2002 2:43PM Prompt 0 0 0 1 0
3 Sep 25 2002 2:43PM quality 0 1 0 0 0
4 Sep 25 2002 3:57PM professionalism 0 1 0 0 0
4 Sep 25 2002 3:57PM Appearance 0 0 0 1 0
4 Sep 25 2002 3:57PM Courteous 0 1 0 0 0
4 Sep 25 2002 3:57PM MetExpectations 0 0 0 1 0
4 Sep 25 2002 3:57PM overallrating 0 1 0 0 0
4 Sep 25 2002 3:57PM Prompt 1 0 0 0 0
4 Sep 25 2002 3:57PM quality 0 0 0 1 0
5 Sep 30 2002 8:11AM professionalism 0 0 0 1 0
5 Sep 30 2002 8:11AM Appearance 0 1 0 0 0
5 Sep 30 2002 8:11AM Courteous 0 0 0 1 0
5 Sep 30 2002 8:11AM MetExpectations 0 0 0 0 1
5 Sep 30 2002 8:11AM overallrating 0 0 0 1 0
5 Sep 30 2002 8:11AM Prompt 0 1 0 0 0
5 Sep 30 2002 8:11AM quality 0 0 0 1 0
6 Oct 16 2002 11:58AM professionalism 0 1 0 0 0
6 Oct 16 2002 11:58AM Appearance 0 0 0 1 0
6 Oct 16 2002 11:58AM Courteous 0 1 0 0 0
6 Oct 16 2002 11:58AM MetExpectations 0 1 0 0 0
6 Oct 16 2002 11:58AM overallrating 0 1 0 0 0
6 Oct 16 2002 11:58AM Prompt 0 1 0 0 0
6 Oct 16 2002 11:58AM quality 0 1 0 0 0
7 Oct 17 2002 7:36AM professionalism 0 1 0 0 0
7 Oct 17 2002 7:36AM Appearance 0 0 0 1 0
7 Oct 17 2002 7:36AM Courteous 0 1 0 0 0
7 Oct 17 2002 7:36AM MetExpectations 0 1 0 0 0
7 Oct 17 2002 7:36AM overallrating 0 1 0 0 0
7 Oct 17 2002 7:36AM Prompt 0 1 0 0 0
7 Oct 17 2002 7:36AM quality 0 1 0 0 0
(35 row(s) affected)
I am trying to generate some totals (sums) for each of the combinations of category (i.e. Appearance, Professionalism) and OUTSTANDING, EXCELLENT, GOOD, AVERAGE, POOR. I want to group the categories and them sum each of the columns (1s).
To do this, I want to run the following SQL statement:
SELECT category, Sum(OUTSTANDING) AS SumOfOUTSTANDING, Sum(EXCELLENT) AS SumOfEXCELLENT, Sum(GOOD) AS SumOfGOOD, Sum(AVERAGE) AS SumOfAVERAGE, Sum(POOR) AS SumOfPOOR
FROM #t
GROUP BY category
My problem is that I’m not sure how to take the results (35 rows above) generated by the stored procedure and use the SELECT SQL statement to generate a result like shown below:
My final result should look like:
category SumOfOUTSTANDING SumOfEXCELLENT SumOfGOOD SumOfAVERAGE SumOfPOOR
Appearance 0 2 0 2 1
Courteous 1 2 0 2 0
MetExpectations 1 1 0 2 1
OverallRating 1 1 0 3 0
Professionalism 1 2 0 2 0
Prompt 0 4 0 1 0
Quality 0 3 0 2 0
I tried to use another stored procedure to create a temporary table and then insert the result set (35 rows) into the temporary table. Then use the SELECT SQL statement above based on the temporary table to get the final result.
I composed the following stored procedure to insert the 35 row result set:
CREATE PROCEDURE myTempTable
AS
CREATE TABLE #myTempTable (
record numeric (9) NOT NULL,
datesubmitted datetime NOT NULL,
Category nvarchar (15) NULL,
OUTSTANDING numeric (9) NULL,
EXCELLENT numeric (9) NULL,
GOOD numeric (9) NULL,
AVERAGE numeric (9) NULL,
POOR numeric (9) NULL
)
INSERT #myTempTable
exec sp_CustSurveyData ‘original stored procedure that generated the 35 rows.
But instead of getting the final result set, I received the following error message in SQL Server 2000:
Server: Msg 8152, Level 16, State 4, Line 0
String or binary data would be truncated.
(0 row(s) affected)
What I do wrong in the process? Any helpful advice would be appreciated.
Thanks