Merge statement with openrowset

  • HI All

    Is it possible to run the MERGE statement using Openrowset or openquery? I tried with below statement:

    SELECT *

    FROM OPENROWSET('SQLNCLI',

    'DRIVER={SQL Server};SERVER=MYSERVER;UID=sa;PWD=mypwd',

    'MERGE dbo.tab1 AS a

    USING (SELECT * FROM [SERVERXYZ].MyDB.dbo.tab1 with (nolock)) AS b

    ON a.Id=b.id

    WHEN MATCHED THEN UPDATE SET

    a.ID=b.id

    WHEN NOT MATCHED BY TARGET THEN

    INSERT(id)

    VALUES(b.id

    )

    WHEN NOT MATCHED BY SOURCE THEN DELETE;')

    but got error:

    Msg 7357, Level 16, State 2, Line 1

    Cannot process the object "MERGE dbo.tab1 AS a

    USING (SELECT * FROM [SERVER2].DotnetNuke2.dbo.tab1 with (nolock)) AS b

    ON a.Id=b.id

    WHEN MATCHED THEN UPDATE SET

    a.ID=b.id

    WHEN NOT MATCHED BY TARGET THEN

    INSERT(id)

    VALUES(b.id

    )

    WHEN NOT MATCHED BY SOURCE THEN DELETE;". The OLE DB provider "SQLNCLI10" for linked server "(null)" indicates that either the object has no columns or the current user does not have permissions on that object.

    Online Trainer For SQL DBA and Developer @RedBushTechnologies with 18 yrs exp.

  • Please check if you have the necessary permission to perform INSERT/UPDATE/DELETE operation on the target table.

  • yes, permissions are there. If i run the merge statement without OPENROWSET, it works.

    So i was querious if it is possible at all?

    Online Trainer For SQL DBA and Developer @RedBushTechnologies with 18 yrs exp.

  • BOL states that OpenRowSet can be used as a Target in the merge but appears to be ambiguous as to whether it can be a source.

    Have you tried loading the data from the openrowset into a temp table then use that as the merge, another alternative could be to use a CTE to do the OpenRowSet load, but It may cause a bottleneck.

    _________________________________________________________________________
    SSC Guide to Posting and Best Practices

  • S_Kumar_S (2/8/2013)


    HI All

    Is it possible to run the MERGE statement using Openrowset or openquery? I tried with below statement:

    You want to run this MERGE as a remote query on SERVER=MYSERVER.

    The MERGE uses a linked server (SERVERXYZ) table as a source.

    What server are you connected to when you run the openrowset query?

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Sorry for replying late. I am connected to MYSERVER while running the openrowset

    Online Trainer For SQL DBA and Developer @RedBushTechnologies with 18 yrs exp.

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

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