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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to get a value from Execution statement
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

itmasterw
Yak Posting Veteran

90 Posts

Posted - 04/11/2013 :  13:50:55  Show Profile  Reply with Quote
Hi,
I have an dynamic sql statement that rturns a percentage which works grate. However, I want to take this value and assign it to a variable. somthing like:
Set @sql = 'Select....From Table'
EXEC @sql

@hold = value of the ececution above EXEC @sql
How can I do this?

Thank you

ITM

Edited by - itmasterw on 04/11/2013 13:51:20

MIK_2008
Flowing Fount of Yak Knowledge

Pakistan
1054 Posts

Posted - 04/11/2013 :  14:16:02  Show Profile  Reply with Quote
well you cannot assign it to a variable, but instead you can assign it to a table ..

declare @sql varchar(1000);declare @x table (col1 int);
set @sql='SELECT 1;'
insert into @x
Exec (@sql)
select * from @x


Cheers
MIK
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

4614 Posts

Posted - 04/11/2013 :  14:16:29  Show Profile  Reply with Quote
Tyy using SP_EXECUTESQL.

For Example:
-- Assuming the datatype is VARCAHR(100)
DECLARE @A VARCHAR(100)
EXEC SP_EXECUTESQL N'SELECT @A = Column ... FROM TABLE', N'@A VARCHAR(100) OUTPUT', @A OUTPUT
PRINT @A
Go to Top of Page

itmasterw
Yak Posting Veteran

90 Posts

Posted - 04/11/2013 :  14:31:38  Show Profile  Reply with Quote
Okay thanks guys, I will try these.
Thank you

ITM
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000