Solved

How can I fill a variable with the results of a scalar query in the same T-SQL script?

Posted on 2009-05-13
13
364 Views
Last Modified: 2012-05-06
I just want to fill a variable declared in my T-SQL script with the results of scalar query that happens inside the same script.

Is this possible with dynamic sql?
0
Comment
Question by:David L. Hansen
  • 5
  • 4
  • 2
  • +2
13 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24380334
Yup, here is an example
declare @sql varchar(1000)
SELECT @sql = COALESCE(@sql+',' ,'') +NAME
FROM master..sysdatabases
print @sql

one more thing, if one of the columns is an integer , make sure that you cast it to varchar like

declare @sql varchar(1000)
SELECT @sql = COALESCE(@sql+',' ,'') +cast(intColumn as varchar )
FROM table
print @sql
0
 
LVL 4

Expert Comment

by:ruffone
ID: 24380335
Is this what your are asking?
declare @personId int
SELECT @personId = personId FROM People WHERE personId =784
 
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 24380374
I think the OP means something like this -- which doesn't work.  I think I've seen a way to accomplish it ... but can't remember the solution.

declare @I int
Declare @SQL varchar(max)
set @SQL = 'Select count(*) from sys.tables'
set @I = exec (@SQL)

Open in new window

0
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)

 
LVL 15

Author Comment

by:David L. Hansen
ID: 24380378
Just to clarify, I want to fill a variable using a dynamic sql query.  I can build and execute the query dynamically right now....but....I want to pass the result into a variable.
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 24380380
Another syntax that fails:

declare @I int
Declare @SQL varchar(max)
set @SQL = 'Select @I= count(*) from sys.tables'
exec (@SQL)

Open in new window

0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 24380383
Yes, Daniel...that's what I want exactly!
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 24380384
sl8rz, have I got the idea you're working on correct?
0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 24380391
Yes, but you're right.  With this approach it doesn't work (syntax).
0
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 500 total points
ID: 24380404
here it is:

http://sqlserver2000.databases.aspfaq.com/how-do-i-get-the-result-of-dynamic-sql-into-a-variable.html

select @ideclare @I int
Declare @SQL nvarchar(max)
set @SQL = 'Select @I= count(*) from sys.tables'
    EXEC sp_executesql
        @statement = @sql,
        @params = N'@i INT OUTPUT',
        @i = @i OUTPUT

select @i
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24380413
0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 24380462
Thank you Daniel!

That's it!
0
 
LVL 15

Author Closing Comment

by:David L. Hansen
ID: 31581256
Awsome work...thank you very much.
0
 
LVL 40

Expert Comment

by:Sharath
ID: 24380480

Hope you got idea from the links provided by DanielWilson and aneeshattingal. Just rephrasing the same as there are some errors in DanielWilson  solution.

declare @I int
Declare @SQL nvarchar(max)
set @SQL = 'Select @cnt = count(*) from sys.tables'
exec sp_executesql @SQL,N'@cnt int output',@cnt = @I output
select @I

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

789 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