By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,685 Members | 1,674 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,685 IT Pros & Developers. It's quick & easy.

Passing a varible as an linked server name in openquery

P: 1
Hi,
I would be glad if anybody can solve my problem.
I have query to be run which requires a linked server to be accessed.
Therefore I am using openquery. Now in the storedprocedure I pass linkserver name as variable and trying to use it in open query but it gives syntax error. If any body can suggest any other method or solution to this problem than it would be of great help

Try this query

Declare @LinkedServer varchar(50)
select * from openquery ( @LinkedServer , 'select * from master.dbo.spt_monitor')

Here instead of variable, if you pass the server name it works fine but if you pass the variable than it gives syntax error.
Feb 25 '08 #1
Share this Question
Share on Google+
1 Reply


deepuv04
Expert 100+
P: 227
Hi,
I would be glad if anybody can solve my problem.
I have query to be run which requires a linked server to be accessed.
Therefore I am using openquery. Now in the storedprocedure I pass linkserver name as variable and trying to use it in open query but it gives syntax error. If any body can suggest any other method or solution to this problem than it would be of great help

Try this query

Declare @LinkedServer varchar(50)
select * from openquery ( @LinkedServer , 'select * from master.dbo.spt_monitor')

Here instead of variable, if you pass the server name it works fine but if you pass the variable than it gives syntax error.

Hi
first build a string query and then execute it..

Declare @LinkedServer varchar(50)
declare @Sql varchar(8000)
set @LinkedServer = 'abc'
set @Sql = 'select * from openquery (' + @LinkedServer + ' , ''select * from master.dbo.spt_monitor'')'

--print @Sql
Exec (@Sql)

thanks
Feb 25 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.