I have 2 db on one server. I need to write procedure that does select basing on @db variable.
I know 2 possibilities for this:
I declare
@SQL nvarchar(max)
and generating my query in plain text. Then i doexec @SQL
. Bad variant imho.I do 2 similar queries and use if @db='' 1st query else 2nd query. Another bad variant because it is code duplicate.
Question is - is there any way to do like this or similar: select * from @db.dbo.table?