I'm trying to run a dynamic script to then return the variable so I can pass in to the rest of my script. I've a couple of ways with the help of Google, but I think I still haven't got the syntax correct, therefore getting error or null value returned.
Can someone please advise where I've gone wrong.
For example: To return the value for variable @table_name ASIA is the database and this is set as variable which is appended to the table name that is retrieved from the table and T5148 is the id from the table to turn the table name as is so a variable. I have set this a variables as this script sits when other scripts which loops
Thank you
declare @table_name nvarchar(50)
declare @database nvarchar(50)
declare @id nvarchar(50)
declare @sql nvarchar(max)
set @database = 'ASIA'
set @id = 'T5178'
set @sql = N'SELECT @table_name = ''@database''+table_name
FROM ''@database''+tables (NOLOCK)
WHERE id = ''@id'''
exec sp_executesql @sql, N'@table_name nvarchar(50) output', @table_name output
select @TRAN_TABLE