SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Needed Help


Needed Help

Author
Message
mail.harish.kala
mail.harish.kala
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 371
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
Stuart Davies
Stuart Davies
SSCertifiable
SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)

Group: General Forum Members
Points: 7441 Visits: 4823
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 Smart way to ask a questionThere 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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search