Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access table in linked server using a query stored in a variable.

Status
Not open for further replies.
Apr 26, 2002
1
US
All,

I am trying to store a query in a variable and then execute the variable. SQL is giving me errors because the table is on a remote server. I have already setup the linked server and tested the code in the Query Analyzer.

The query itself works fine, but when I load the query into the variable and try to execute it, SQL chokes. I would try using OPENQUERY, but it does not accept variables for its arguments. Any suggestions?

SET @vchQuery =
'
SELECT
FNAME, LNAME, EMAIL, PHONE
FROM
remoteserver.database.owner.table AS table
WHERE
table.id = 1885
'
EXECUTE
@vchQuery

Thanks in advance for your help.

-DJ
 
SQL chokes? Does it give you a more precise error message?

I suspect that the error may be...

Server: Msg 7202, Level 11, State 2, Line 3
Could not find server ....

Enclose the Execute variable in parenteheses.

EXECUTE (@vchQuery)
Terry L. Broadbent - DBA
Computing Links:
faq183-874 contains "Suggestions for Getting Quick and Appropriate Answers" to your questions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top