How to use OpenRowSet?

  • Hi experts,

    Can I join these (showing at the bottom of the question) 2 queries together?  The first query is from server AMKSQL1, the 2nd query data is in AMKSQL2 but retrived from AMKSQL1 by using OPENROWSET.

    I want to retrieve VendName in query 1 and the key is VendorID.

    If it's from 2 tables in same database, the query will be like:

    SELECT J.Job_ID, J.J_Open_Date, J.J_Priority,J.J_Vendor_ID, P.VendName FROM Job_Tracking_Table J, PM00200 P where J.J_VendorID=P.VendorID

    but can I use OpenRowset to combine the following 2 queries into 1 query??? 

    QUERY1:

    SELECT J.Job_ID, J.J_Open_Date, J.J_Priority,J.J_Vendor_ID FROM Job_Tracking_Table J

    QUERY2:SELECT P.VendorID, P.VendName FROM OPENROWSET('SQLOLEDB','AMKSQL2';'abc';'cde', 'SELECT * FROM HFC.dbo.PM00200 ') AS P

    Please show me how.Thank you.

  • Join the two queries together, you can reference the result of the OPENROWSET as a table

    SELECT J.Job_ID, J.J_Open_Date, J.J_Priority, J.J_Vendor_ID, P.VendName

    FROM Job_Tracking_Table J

    INNER JOIN OPENROWSET('SQLOLEDB','AMKSQL2';'abc';'cde', 'SELECT VendorID, VendName FROM HFC.dbo.PM00200') P

    ON P.VendorID = J.J_Vendor_ID

    I am not sure on this but I think sql will pull all the data from the OPENROWSET before joining so you should retrieve the smallest amount of data required.

    Far away is close at hand in the images of elsewhere.
    Anon.

  • Thank you very much, it works.

Viewing 3 posts - 1 through 2 (of 2 total)

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