Reputation: 474
Can anybody tell me how i can do this?
declare @test varchar(20)
set @test ='DatabaseName'
select b.* from @test.dbo.Table_Name
Here i am taking database from variable and using it in query.
Shall do this?
Upvotes: 1
Views: 437
Reputation: 138960
declare @test varchar(20)
set @test ='DatabaseName'
declare @SQL nvarchar(max)
set @SQL = 'select b.* from '+quotename(@test)+'.dbo.Table_Name as b'
exec (@SQL)
Upvotes: 1
Reputation: 176896
Make use of Exec
command or Sp_executesql
because you are building dynamic query.
Upvotes: 1