November 24, 2012 at 3:42 pm
Does anyone know a way of running a MERGE statement across 2 tables over a linked server?
Getting the message "The target of a MERGE statement cannot be a remote table, a remote view, or a view over remote tables."
November 24, 2012 at 6:08 pm
Try executing the whole statement at destination db:
EXEC('MERGE ...') AT MYDBLINKNAME
What is remote server? Sql server, oracle or something else?
November 25, 2012 at 6:00 am
Thanks for the suggestion, i'll give it a try.
This is SQL 2008R2 setup I'm trying to run it on.
November 26, 2012 at 1:45 pm
Tried it yesterday and it worked perfectly. Thanks again.
November 26, 2012 at 3:57 pm
You're welcome!
April 23, 2013 at 8:51 am
how does the query works?
April 23, 2013 at 8:57 am
girl_bj0619 (4/23/2013)
how does the query works?
Execute At [linked_server]
causes the statement to be executed on the linked server (not on the server running the EXECUTE statement). Is that what you mean?
--Edit: fix typo
April 23, 2013 at 10:40 am
i meant where to apply it?
here..?
merge ...
using....
April 23, 2013 at 10:48 am
girl_bj0619 (4/23/2013)
i meant where to apply it?here..?
merge ...
using....
Can you take a bit more time to write out what you are trying to do please, because I do not understand your question?
April 23, 2013 at 11:16 am
I think you are asking where do you run the MERGE???
EXEC('Your Entire MERGE Statement goes here') at LinkServer
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
April 23, 2013 at 8:00 pm
i tried this but i get this error:
The target of a MERGE statement cannot be a remote table, a remote view, or a view over remote tables.
merge to linked server which is tableA
MERGE [...].[...].[...].tableA AS B
USING tableB AS A with (nolock)
ON A.col1=B.col1
AND A.col2=B.col2
WHEN MATCHED THEN
UPDATE
SET B.col1= A.col1,
B.col2=A.col2,
B.col3=A.col3
WHEN NOT MATCHED THEN
INSERT(col1,col2,col3)
VALUES(A.col1,A.col2,A.col3);
April 23, 2013 at 8:03 pm
i tried this but i get this error:
The target of a MERGE statement cannot be a remote table, a remote view, or a view over remote tables.
merge to linked server which is tableA
MERGE [...].[...].[...].tableA AS B
USING tableB AS A with (nolock)
ON A.col1=B.col1
AND A.col2=B.col2
WHEN MATCHED THEN
UPDATE
SET B.col1= A.col1,
B.col2=A.col2,
B.col3=A.col3
WHEN NOT MATCHED THEN
INSERT(col1,col2,col3)
VALUES(A.col1,A.col2,A.col3);
April 24, 2013 at 4:10 am
Are your source and target tables on different servers?
The MERGE statement won't work in this scenario - you need both the tables to be on the same SQL instance, I believe.
April 24, 2013 at 9:05 am
girl_bj0619 (4/23/2013)
i tried this but i get this error:The target of a MERGE statement cannot be a remote table, a remote view, or a view over remote tables.
merge to linked server which is tableA
MERGE [...].[...].[...].tableA AS B
USING tableB AS A with (nolock)
ON A.col1=B.col1
AND A.col2=B.col2
WHEN MATCHED THEN
UPDATE
SET B.col1= A.col1,
B.col2=A.col2,
B.col3=A.col3
WHEN NOT MATCHED THEN
INSERT(col1,col2,col3)
VALUES(A.col1,A.col2,A.col3);
You are referencing your target table (tableA) using 4 part naming convention (server.db.schema.object), so it probably looks to SQL Server as if you're attempting to MERGE into a remote table. Insure that you're running the MERGE statement locally on the same server as your target table, and re-write your reference to target table using 3 or 2 part naming convention like this:
MERGE [db].[schema]. AS B
"Do not seek to follow in the footsteps of the wise. Instead, seek what they sought." - Matsuo Basho
Viewing 14 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy