OpenQuery help

  • Hi

     

    How can i schedule Open Query? I am using SQL SERVER 2000 and running my open query on AS400. i need to store query result in a table on sql server and need to schedule this query ...

    Any help please

     

  • Have you got your linked server running for an interactive query that you can run?  If so, then you can use the same code to make a job for SQL Server Agent.  Look in Enterprise Manager in SQL 2000 under the "Management" section on the left.  Make a job with a single step that executes T-SQL code - paste your code in there.  Schedule the job as appropriate.  Remember that you may need to set the user under which the job executes appropriately to ensure that it has access to the linked server.

  • Thanks Ian Yates It's working

    I am working on OpenQuery First time so could you please let me know how to pass parameter to OpenQuery?

    Regards

     

  • OpenQuery does not accept parameters/variables.  You'll need to do something ugly with dynamic SQL for it to work.

    EG

    set @query = 'select * from myTable where myColumn=''xyz'' '    (note the double single quotes)
    set @fullQuery = 'select * from openQuery(myLinkedServer, ''' + replace(@query, '''', '''''') + ''')'
    print @query
    print @fullQuery
    exec(@fullQuery)

    Looks awful

     

  • Thanks Ian

     

    Realy Helpful..........

  • Afternoon Ian

    I am trying to do much the same thing .In your query you have

    set @fullQuery = 'select * from openQuery(myLinkedServer, ''' + replace(@query, '''', '''''') + ''')'

    but would I would like to use a variable instead of substituting 'mylinkedsever' for a fixed value ,

    e.g.

    set @fullQuery = 'select * from openQuery(@server , ''' + replace(@query, '''', '''''') + ''')'

    Tried various combinations but to no avail.

    Thanks

    Geoff

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

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