Solved

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

Posted on 2013-10-22
5
533 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now