May 10, 2013 at 1:11 am
declare @LinkedServer varchar(15)
declare @DatabaseName varchar(20)
declare @SQL varchar(250)
set @LinkedServer = '[SURE]'
set @DatabaseName = 'master'
--select * from @LinkedServer.@DatabaseName.sys.databases
set @SQL = 'select * from '+@LinkedServer+'.'+@DatabaseName+'.sys.databases'
print @SQL
exec @SQL
---- Result
select * from [SURE].master.sys.databases
Msg 203, Level 16, State 2, Line 10
The name 'select * from [SURE].master.sys.databases' is not a valid identifier.
Working Perfectly:
select * from[SURE].master.sys.databases
May 10, 2013 at 1:27 am
Hi - you've got some missing brackets here. You can either use:-
exec (@SQL)
or
exec sp_executesql @SQL
If you use the second method you will have to declare @sql as ntext, nchar or nvarchar
-------------------------------Posting Data Etiquette - Jeff Moden [/url]Smart way to ask a question
There are naive questions, tedious questions, ill-phrased questions, questions put after inadequate self-criticism. But every question is a cry to understand (the world). There is no such thing as a dumb question. ― Carl Sagan
I would never join a club that would allow me as a member - Groucho Marx
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply