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 2008 Forums
 Transact-SQL (2008)
 How to get a value from Execution statement

Author  Topic 

itmasterw
Yak Posting Veteran

90 Posts

Posted - 2013-04-11 : 13:50:55
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

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2013-04-11 : 14:16:02
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
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2013-04-11 : 14:16:29
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 - 2013-04-11 : 14:31:38
Okay thanks guys, I will try these.
Thank you

ITM
Go to Top of Page
   

- Advertisement -