Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Using OPENQUERY passing a variable for the query.
Message
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
00457919
Message ID:
00458060
Views:
20
Hi!

Somewhere here I already asked this question and investigated this and posted unswer.

The solution - use EXEC[UTE] command to execute query in the variable. This variable than could be prepared using concatenations to look like required for OPENQUERY, buut you can just use parameters in such dynamic query just by putting parameter values in appropriate places.

However, to debug and develop I made SP without use of parameters (fixed parameters) first, because than it is complex to debug string - looks very awful.


>I'm querying a linked server ( in this case, Visual Foxpro).
>The query:
>
>SELECT * FROM OPENQUERY(QTII_SYS_FOX,'SELECT LNAME FROM PATIENT WHERE PATIENTID = "1234567"')
>
>gives me the results I want. However, I want to build the query dynamically at run time. When I have a variable called @SQLSTMNT that is equal to 'SELECT LNAME FROM PATIENT WHERE PATIENTID = "1234567"', I try to do the OpenQuery as
>'SELECT * FROM OPENQUERY(QTII_SYS_FOX, @SQLSTMNT)'.
>But I get a syntax error. Everything I've tried seems to indicate that SQL Server is simply not prepared to have anything except a literal there. Can anybody help me?
>
>Thanks.
Vlad Grynchyshyn, Project Manager, MCP
vgryn@yahoo.com
ICQ #10709245
The professional level of programmer could be determined by level of stupidity of his/her bugs

It is not appropriate to say that question is "foolish". There could be only foolish answers. Everybody passed period of time when knows nothing about something.
Previous
Reply
Map
View

Click here to load this message in the networking platform