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
362 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
 
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

911 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

21 Experts available now in Live!

Get 1:1 Help Now