Solved

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

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

717 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