I have a 3 tables in my database. They are
Table_1
Table_2
General_table
select * from General_table
id Col1
1 34
2 35
9 34
I have written a dynamic query where in I need to extract contents in Table_1 and Table_2. So
declare @fil nvarchar(max) = '34'
declare @new_var nvarchar(max)
set @new_var = 'select id from General_table where col1 =' + ''''+@fil+''''
print('select * from Table_'+@new_var+'')
When I execute this, I get below output
But the expected output is to be dynamically calling Table_1
Expected output
select * from Table_1
Similarly, when I pass @fil as 35, the expected output should be
select * from Table_2
1条答案
按热度按时间wfypjpf41#
Does the following snippet help build the SQL String you are expecting?