July 26, 2006 at 4:52 am
I have a dynamically builded query, for example 'select Name from SomeTable where ID = 123'. This returns 'SomeName' - that is, it returns only one value.
Now, I want to execute this query with exec sp_ExecuteSql @sql and I want to put the given result (that is 'SomeName') in another, previously declared variable. How do I do that?
July 26, 2006 at 5:05 am
Here's an example...
--data
create table SomeTable(ID int, Name varchar(10))
insert SomeTable
select 1, 'Bob'
union all select 123, 'Jim'
--calculation
declare @sql nvarchar(100)
set @sql = N'select @Name = Name from SomeTable where ID = 123'
declare @Name varchar(10)
exec sp_ExecuteSql @sql, N'@Name varchar(10) output', @Name output
select @Name
--tidy up
go
drop table SomeTable
/*results
----------
Jim
*/
Ryan Randall
Solutions are easy. Understanding the problem, now, that's the hard part.
July 26, 2006 at 5:54 am
Thanks a million, Ryan
That solved all of my troubles!!! You really made my day!
July 26, 2006 at 5:57 am
I'm happy to help. Thanks for the feedback
Ryan Randall
Solutions are easy. Understanding the problem, now, that's the hard part.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply