How to use OPENQUERY() with a local variable?

I want to make query to multiple servers and the query is run on remote server. So that, I tired that the following code.

--(The @svr_name will change during program runtime.)
DECLARE @svr_name char(20)
SET @svr_name = 'SERVER1'
SELECT * FROM OPENQUERY(@svr_name, 'SELECT * FROM LIBRARY_DB.DBO.MEMBER')

It does not work!
What can I do?
Thanks
Nelson
nelsonyuenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nelsonyuenAuthor Commented:
Adjusted points to 50
0
Jon_RaymondCommented:
Did you check OPENQUERY(T-SQL) in Books OnLine?  You need to use sp_addlinkedserver to set the server name.  Here is an example:

This example assumes that an Oracle database alias called ORCLDB has been created.

EXEC sp_addlinkedserver 'OracleSvr',
    'Oracle 7.3’,
    'MSDAORA',
    'ORCLDB'
GO

SELECT *
FROM OPENQUERY(OracleSvr, 'SELECT name, id FROM joe.titles')
GO
0
SunnybazCommented:
can you do this with sql server, instead of oracle ? if you have a sql server registered in enterprise manager, can you use open query the same way ?

0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Jon_RaymondCommented:
Yes, exactly. It doesn't matter.
0
Jon_RaymondCommented:
Try this:

DECLARE @svr_name char(20)
SET @svr_name = 'SERVER1'

EXEC sp_addlinkedserver @svr_name,
    'SQL Server',
    'SQLOLEDB',
    'Library_DB'

SELECT * FROM OPENQUERY(@svr_name, 'SELECT * FROM LIBRARY_DB.DBO.MEMBER')
0
nelsonyuenAuthor Commented:
Hi, Raymond
I know what you mean, but actually my posted code and yours also have syntax error in Query Analyzer.
Also I tell everyone that the remote server is already linked and tested with the following code.

SELECT * FROM OPENQUERY(SERVER1, 'SELECT * FROM LIBRARY_DB.DBO.MEMBER')

This works, but the problem is that OPENQUERY() cannot accept the server name, that is a variable as my question coded.

0
Jon_RaymondCommented:
Sorry, I wasn'table to test it.  How about putting the SQL statement into a variable and then executing it like this.  I tested this one ok:

DECLARE @svr_name char(20)
DECLARE @sSQL varchar(200)

SET @svr_name = 'SERVER1'
SET @sSQL = 'SELECT * FROM OPENQUERY (' + @svr_name + ',"SELECT * FROM DBO.MEMBER"' + ')'
EXEC (@ssql)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nelsonyuenAuthor Commented:
Thank you very much!
It works now
:-)

Nelson
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.