Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
Medium Priority
?
368 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
[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
  • 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
Technology Partners: 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!

 
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 2000 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 41

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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 …
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

618 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