?
Solved

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

Posted on 2013-10-22
5
Medium Priority
?
561 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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 66

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 66

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

777 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