x

Dynamic sql

SET @SQL='select @parm1= COUNT(*) from '+@Database+'.dbo.FactPersonSnapshot f group by f.EnrollmentMonthDateKey order by f.EnrollmentMonthDateKey '

exec sp_executesql @SQL, @params = N'@parm1 int OUTPUT', @parm1=@parm1 OUTPUT
select @parm1

This query has to return multiple rows. Instead it is just outputting the first row. Could anyone pls help.

Thanks vs0523
more ▼

asked Jul 09, 2012 at 06:29 PM in Default

vs0523 gravatar image

vs0523
50 3 5 7

(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

It looks like you are after a count for each month. Because @parm1 is an int, it can't hold multiple counts. You'll need to store your results in a table that can hold the rows.

Try something like this (I've used sys.all_objects so anybody can run the example, but you can just put your query back in there):

DECLARE @sql NVARCHAR(4000)

CREATE TABLE #enrollmentCounts (enrollmentCount int, enrollmentDateKey sysname)

SET @sql = 'insert into #enrollmentCounts select COUNT(*), type from sys.all_objects group by type'
exec sp_executesql @sql

SELECT * FROM #enrollmentCounts

DROP TABLE #enrollmentCounts
more ▼

answered Jul 09, 2012 at 07:07 PM

KenJ gravatar image

KenJ
20k 1 3 11

(comments are locked)
10|1200 characters needed characters left

The query is returning the count of rows from the table - this is a one row, one column output. Plus you are then setting the value to an integer valued output parameter.

What multiple rows are you expecting?
more ▼

answered Jul 09, 2012 at 06:55 PM

Kev Riley gravatar image

Kev Riley ♦♦
53k 47 49 76

The query returns count of rows for each month(it is a group by month).This is the query that I tried to modify as above:

SET @SQL='select EnrollmentMonthDateKey,COUNT(*) as MemberCount from '+@Database+'.dbo.FactPersonSnapshot f group by f.EnrollmentMonthDateKey order by f.EnrollmentMonthDateKey '

EXEC (@SQL)

I was able to get multiple rows with this. But when I am assigning this to variable it is just returning the first row.
Jul 09, 2012 at 07:07 PM vs0523
Sorry - was speed reading on my phone! The reason still stands regarding the single variable though. Ken has shown a way of getting multiple records out.
Jul 09, 2012 at 08:06 PM Kev Riley ♦♦
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x38

asked: Jul 09, 2012 at 06:29 PM

Seen: 760 times

Last Updated: Jul 09, 2012 at 08:06 PM