Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Results of a query to a variable
Message
From
22/05/2003 15:27:22
John Deupree
Long Term Care Authority
Tulsa, Oklahoma, United States
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
00791809
Message ID:
00791862
Views:
14
I don't seem to make it work. See below. What is the purpose of the output param being listed twice?

Thanks
declare @id nvarchar(50), @sql nvarchar(500), @table varchar (50)
set @table = 'intake'
set @sql = N'select in_lname from ' + @table + ' where in_id = 7'

exec sp_executesql @sql, N'@id nvarchar(50) output', @id output

if @id is null
  print 'null'
else
  print @id
Prints null every time.

>See How to return a value from dynamicaly executed T-SQL code FAQ #8130
>
>>I have a stored proc that uses the following code:
>>
>>set @var = (select col from table where pk = @param)
>>
>>
>>What I would like to do is make 'table' a variable to make the sp more generic. Something like this would be nice (but this won't work).
>>
>>set @sql = 'select col from ' + @table + ' where pk = ' + @param
>>set @var = (exec sp_executesql @sql)
>>
>>
>>Any ideas how to set a variable to the result of a dynamically constructed sql statement?
>>
>>TIA
>>
>>John
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform