sql server - How can I use SQL to get a variable value from a literal? -
part 1
declare @a int declare @b nvarchar(20) set @a=123 set @b='@a'
part 2
declare @sql nvarchar(max) set @sql='select ' + @b exec sp_executesql @sql --should return 123
directly referencing @a in non-dynamic sql not acceptable task.
the above in part 2 generically trying do. understand variable out of scope , won't work done above. how use @b value of @a?
i didn't understand whole thing asking, can define variables on sp_executesql
:
exec sp_executesql @sql, n'@a int', @a = @a
Comments
Post a Comment