Solved

Result of a dynamic select into a variable in SQL Server 2005

Posted on 2013-10-22
5
540 Views
Last Modified: 2013-10-23
I need to build a select (all that in a stored procedure) based on several conditions passed to the procedure
The select should return the result in a local variable
Something like this

declare @Q varchar(1000)
declare @R integer
select @Q = 'select @R = count(*) from Lots' -- @R is an output argument
exec (@Q)


This fails (@R must be declared) on the exec
Is there another way not being simply this : select @R = count(*) from Lots'

(I need this @Q because it is being build at several places
0
Comment
Question by:LeTay
  • 2
  • 2
5 Comments
 
LVL 22

Expert Comment

by:Steve Wales
ID: 39591774
Found this:
stackoverflow.com/questions/14399020/storing-value-in-variable-using-dynamic-sql-in-sql-server

Tested it on my test server and it seems to work just fine.

Good luck, hope that's helpful.
0
 
LVL 9

Accepted Solution

by:
sachinpatil10d earned 500 total points
ID: 39591791
Try this one

declare @Q nvarchar(1000)
declare @R integer
select @Q = N'select @R = count(*) from Lots' -- @R is an output argument 
exec sp_executesql @Q,N'@R integer OUTPUT', @R OUTPUT
print @R

Open in new window

0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39591797
sjwales - Just so you know, EE is cracking down on what it refers to as 'blind links' essentially a link to a competing site with no context other than 'go here'.  To avoid any conflicts, please add a little color commentary when giving an 'go here' answer.  TIA.
0
 
LVL 22

Expert Comment

by:Steve Wales
ID: 39591836
jimhorn - thanks for the heads up.  Did a bit of a search in the Community Support area on this stance and have seen the why's and wherefore's behind the stance and it's understandable.

sachinpatil10d's answer does the same thing though, so the OP has his answer if mine gets deleted by admin.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39591896
Yep.  The 'add color to the link' is kind of the happy medium they came up with.   Also, this applies only to competitors sites which have a higher chance of going bad (or being a big pile of goo).  Manufacturers sites such as TechNet are considered okay.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question