Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 how to get result of a query in a variable

Author  Topic 

khufiamalik
Posting Yak Master

120 Posts

Posted - 2008-08-13 : 05:27:51
Hello All,

I have a query in a variable and i want to store it's resultant in another variable , how can I do it

My Query is like 'Select EmployeeSalary from Employee'

Waiting for your kind reply
regards
Yasir Mehmood

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-13 : 05:29:19
Select @yourvariable=EmployeeSalary from Employee

but make sure the query is returning only a single value as variable can hold only a single value not a resultset.
Go to Top of Page

khufiamalik
Posting Yak Master

120 Posts

Posted - 2008-08-13 : 05:31:43
actullay query is being passed from some external module and it is complete query including SELECT statement.

Is there any other way to do this?
Go to Top of Page

bhuvnesh.dogra
Starting Member

22 Posts

Posted - 2008-08-13 : 06:53:04
Below is your solution but you nedd to execute all stuff in one transaction..



-------------------------------------------------
declare @tbl_variable table (nam varchar(40))
insert into @tbl_variable
SELECT top 10 [name] FROM SYSOBJECTS ORDER BY [name] COLLATE Latin1_General_CI_AI
select * from @tbl_variable
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-13 : 07:45:18
or use # table
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2008-08-13 : 07:56:30
quote:
Originally posted by bhuvnesh.dogra

Below is your solution but you need to execute all stuff in one transaction..

-------------------------------------------------
declare @tbl_variable table (nam varchar(40))
insert into @tbl_variable
SELECT top 10 [name] FROM SYSOBJECTS ORDER BY [name] COLLATE Latin1_General_CI_AI
select * from @tbl_variable



Sorry bhuvnesh.dogra I don't see how this will help OP unless this SELECT top 10 [name] FROM SYSOBJECTS ORDER BY [name] COLLATE Latin1_General_CI_AI happens to be the query he wants (which I don't think it is)

He's getting a dynamic query back from something as a string. So he'll have to execute that dynamically. Therefore he can't use a table variable because it won't exists in the scope of his dynamic call.

visakh16 : Yes he could use a temp table but it's not what he's after.

khufiamalik - someone raised exactly this problem a few hours ago and I explained a solution to them.

Check out http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=108730

-------------
Charlie
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2008-08-13 : 08:13:55
IN FACT IT WAS YOU!

Duplicate posting. Please see

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=108730

-------------
Charlie
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-13 : 09:56:48
quote:
Originally posted by Transact Charlie

IN FACT IT WAS YOU!

Duplicate posting. Please see

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=108730

-------------
Charlie


oh...just realised what he was after. sorry i missed the other thread
Go to Top of Page
   

- Advertisement -