Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Merge statement with openrowset


Merge statement with openrowset

Author
Message
S_Kumar_S
S_Kumar_S
SSC-Addicted
SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)

Group: General Forum Members
Points: 414 Visits: 1062
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.

-----Table Proc Index Performance TSQL &&%$#@*(#@$%.......------------
Deep Into SQL Jungle
arun1_m1
arun1_m1
SSC Eights!
SSC Eights! (915 reputation)SSC Eights! (915 reputation)SSC Eights! (915 reputation)SSC Eights! (915 reputation)SSC Eights! (915 reputation)SSC Eights! (915 reputation)SSC Eights! (915 reputation)SSC Eights! (915 reputation)

Group: General Forum Members
Points: 915 Visits: 612
Please check if you have the necessary permission to perform INSERT/UPDATE/DELETE operation on the target table.
S_Kumar_S
S_Kumar_S
SSC-Addicted
SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)

Group: General Forum Members
Points: 414 Visits: 1062
yes, permissions are there. If i run the merge statement without OPENROWSET, it works.
So i was querious if it is possible at all?

-----Table Proc Index Performance TSQL &&%$#@*(#@$%.......------------
Deep Into SQL Jungle
Jason-299789
Jason-299789
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1132 Visits: 3229
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
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8957 Visits: 19014
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
Exploring Recursive CTEs by Example Dwain Camps
S_Kumar_S
S_Kumar_S
SSC-Addicted
SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)

Group: General Forum Members
Points: 414 Visits: 1062
Sorry for replying late. I am connected to MYSERVER while running the openrowset

-----Table Proc Index Performance TSQL &&%$#@*(#@$%.......------------
Deep Into SQL Jungle
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search