Openquery and using variables

  • Microsoft website states "OPENQUERY does not accept variables for its arguments". Is there a workaround it?

    What I am trying to achieve is this:

    SELECT * FROM openquery (@myserver, @myquery)

    I have set the variable @myserver as nvarchar datatype.

    Basically, I dont want to hardcode the linked servername or catalog name or schema name in the query.

    Is it possible? Any ideas? Thanks

  • Have you tried dynamic sql?

    Is there a good reason why you don't want to hardcode the query?

  • My Linked Server name and sql query change. Even if I write multiple queries to fulfill all the conditions, I am still left with the linked server name as a variable?

    The latest thing is to use four-part name. Can I supply variables to do that job?

    We do not want the server locations to be hard coded.

    1) Any ideas about that?

    2) Do u think sp_addlinkedserver could solve the problem?

     

  • I think you'll be stuck with dynamic sql in this situation... unless you want to create one sp per linked server and then dynamic sql in each sp... I think you're better off just coding the sql query in the program and fire it up from there (just make sure you're protected against sql injection).

    I however have very little experience in this situation so I welcome any gurus to point out better solutions.

  • I think also that dynamic SQL is the solution with a string contructed and executed with either a four part naming or an openquery syntax



    Bye
    Gabor

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply