I am trying to create a report in an ASP VB .Net application. My stored procedure tallies results from a survey which our website members fill out. It returns multiple result sets. The first is a summary table which includes a count of all the respondents. The next 8 result sets are from a pivot table for each question asked, which list the question and then the count of answers supplied.
When I run the stored procedure in SQL Management Studio, I get the result sets with the correct tabulations, but whenever I call the procedure from VB .NET using a data adapter to fill a dataset, I get the right number of tables with the correct columns. I even get the correct data for any field that isn't calculated. All the counted fields return 0.
I have even tried stuffing the results into XML and then selecting the XML within the stored procedure, with the same results.
Below is my system setup and my stored procedure. Any help is greatly appreciated.
Microsoft SQL Server Management Studio v11.0.3128.0
Microsoft Analysis Services Client Tools v11.0.3128.0
Microsoft Data Access Components (MDAC) v6.2.9200.16384
Microsoft MSXML v3.0 6.0
Microsoft Internet Explorer v9.10.9200.16721
Microsoft .NET Framework v4.0.30319.18051
Operating System v6.2.9200
Microsoft SQL Server Express (64-bit) v11.0.2100.60
CREATE PROCEDURE [dbo].[SurveyResponsesTallyCAS]
@presenterID int = 0,
@startDate datetime = NULL,
@endDate datetime = NULL,
@facility varchar(100) = NULL
AS
BEGIN
DECLARE @surveyID int, @SurveyName varchar(256), @Presenter varchar(256)
SET @SurveyName = 'CAS'
SET @surveyID= (SELECT Survey.ID FROM Survey WHERE Survey.Name = @SurveyName)
IF @presenterID = 0
SET @Presenter = 'All Presenters'
ELSE
SET @Presenter = (SELECT name FROM Presenter WHERE ID = @presenterID)
SET NOCOUNT ON;
DECLARE @Question varchar(256),
@Responses nvarchar(max),
@ParamDefs nvarchar(max),
@SQL nvarchar(max),
@responseXML xml;
SET @ParamDefs = N'@Question varchar(256), @Responses nvarchar(512)';
SET @ParamDefs = @ParamDefs + N', @presenterID int, @startDate datetime, @endDate datetime, @facility varchar(100)';
SET @ParamDefs = @ParamDefs + N', @surveyID int, @responseXML xml OUTPUT';
DECLARE @indexQuestions int;
SET @indexQuestions = 0;
--Return the summary data: Survey name, presenter, total respondents, date range of received surveys
SELECT @SurveyName AS [Survey], @Presenter AS [Presenter]
, (CAST(COUNT(SurveyHeader.SurveyID) as int)) AS [Total Responses]
, @startDate AS [StartDate], @endDate AS [EndDate]
FROM SurveyHeader
WHERE SurveyHeader.SurveyID = @surveyID
AND (SurveyHeader.ID IN (SELECT L.SurveyHeaderID FROM PresenterSurveyLookup L
WHERE (L.PresenterID=@presenterID OR @presenterID=0)))
AND (SurveyHeader.SurveyID=@surveyID)
AND (SurveyHeader.DateAdded >= @startDate OR @startDate IS NULL)
AND (SurveyHeader.DateAdded < DATEADD(day, 1, @endDate) OR @endDate IS NULL)
AND (SurveyHeader.Facility = @facility OR @facility IS NULL)
-- loop through each question and get tallied results
WHILE 1 = 1
BEGIN
SELECT TOP 1 @indexQuestions = QuestionOrder, @Question = Question
FROM SurveyQuestionsResponses
WHERE SurveyID=@surveyID AND QuestionOrder > @indexQuestions
ORDER BY QuestionOrder
if @@rowcount = 0
begin
break
end
-- Must dynamically create columns for the pivot table. Each question can have different responses and number of responses.
SELECT @Responses =
STUFF(
(
select ',[' + Response +']' FROM [SurveyTool].[dbo].[SurveyQuestionsResponses]
where SurveyID = @surveyID AND Question = @Question ORDER BY ResponseOrder
for xml path('')
),
1,1,'')
SELECT @SQL = N'
SET @responseXML = (select
*
from (
SELECT SurveyQuestionsResponses.Question, SurveyQuestionsResponses.Response
, COALESCE(t.Tally, 0, t.Tally) AS [Tally]
FROM SurveyQuestionsResponses LEFT JOIN (SELECT SurveyDetail.Question, SurveyDetail.Response, COUNT(SurveyDetail.Response) AS [Tally] FROM [SurveyTool].[dbo].[SurveyDetail]
inner join [SurveyTool].[dbo].SurveyHeader on SurveyHeader.ID = SurveyDetail.SurveyHeaderID
inner join [SurveyTool].[dbo].Survey on Survey.id = SurveyHeader.SurveyID
where Survey.ID = @surveyID AND SurveyDetail.Question = @Question
AND (SurveyHeader.ID IN (SELECT L.SurveyHeaderID FROM PresenterSurveyLookup L
WHERE (L.PresenterID=@presenterID OR @presenterID=0)))
AND (SurveyHeader.SurveyID=@surveyID)
AND (SurveyHeader.DateAdded >= @startDate OR @startDate IS NULL)
AND (SurveyHeader.DateAdded < DATEADD(day, 1, @endDate) OR @endDate IS NULL)
AND (SurveyHeader.Facility = @facility OR @facility IS NULL)
group by SurveyDetail.Question, SurveyDetail.Response) AS t
on SurveyQuestionsResponses.Question = t.Question AND SurveyQuestionsResponses.Response = t.Response
WHERE SurveyQuestionsResponses.Question = @Question
) AS SourceTable
PIVOT (
Max(Tally)
FOR Response
IN (
' + @Responses + '
)
) AS PivotTable FOR XML AUTO)'
exec sp_executesql @SQL, @ParamDefs
, @Question = @Question
, @Responses = @Responses
, @surveyID = @surveyID
, @presenterID = @presenterID
, @startDate = @startDate
, @endDate = @endDate
, @facility = @facility
, @responseXML = @responseXML OUTPUT
--Return the current question tally
select @responseXML;
END
END