>>MERGE is brand new to me
yes, new for SQL 2008. I haven't used it yet, so am trying to learn from Aneesh's post.
Main Topics
Browse All TopicsHow do I ouput the results of a MERGE statement such that I can SET IDENTITY_INSERT on the target table and then insert?
MERGE is brand new to me, I'm basically playing with 'MERGE SQL statement - Part 2'
from here: http://www.mssqltips.com/t
IF EXISTS but different, I want to udpate.
IF NOT EXISTS, I want to insert.
IF TARGET has data that is not in SOURCE, I want to delete.
(ridiculously urgent)
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
beautiful aneesh! the last one --
--When there is a row that exists in target table and
--same record does not exist in source table
--then delete this record from target table
will this suffice, down at the bottom, for the DELETE TARGET WHERE NOT EXISTS SOURCE, and how's my OUTPUT? is that ok?
--When exists in target table but not source table then delete this record from target table
WHEN NOT MATCHED BY SOURCE THEN
DELETE
--$action specifies a column of type nvarchar(10)
--in the OUTPUT clause that returns one of three
--values for each row: 'INSERT', 'UPDATE', or 'DELETE',
--according to the action that was performed on that row
OUTPUT $action,
DELETED.ProductID AS TargetProductID,
DELETED.ProductName AS TargetProductName,
DELETED.Rate AS TargetRate,
INSERTED.ProductID AS SourceProductID,
INSERTED.ProductName AS SourceProductName,
INSERTED.Rate AS SourceRate;
SELECT @@ROWCOUNT;
SET IDENTITY_INSERT Products OFF;
No. When I said 'my OUTPUT doesn't include UPDATED.ProductID...'
I was referring to the OUTPUT $action:
OUTPUT $action,
DELETED.ProductID AS TargetProductID,
DELETED.ProductName AS TargetProductName,
DELETED.Rate AS TargetRate,
INSERTED.ProductID AS SourceProductID,
INSERTED.ProductName AS SourceProductName,
INSERTED.Rate AS SourceRate;
the UPDATES were in the actual OUTPUT results. so i was just curious why that is
I fixed that flaw. attempting first run now. back shortly w/details. i would love to show you, if you don't mind, aneesh
wow. it f***** worked. wow
serious wow
ok. i have to run to a meeting. back shortly to award and close.
but i'd also like to show you... the code is below. remember, it's my first one. any insight or direction is HUGELY appreciated.
my counts before:
primary 565753
redundancy 564436
missing 1335
orphans 18
my counts after:
primary 565753
redundancy 565753
missing 0
orphans 0
runtime was :13
my SOURCE and TARGET are on two different servers, and in two physically different buildings
your thoughts?
curious, aneesh, i tired to do the MERGE with a date parm, only intending to insert into target if missing, or delete from target, if orphans exist. it ran waaaaay long, and blew out a bunch of data incorrectly, from the target
can i use a date in the MERGE ?
this suggest i can, but surely i've made an oversight
http://technet.microsoft.c
field1 & field2 are the unique key. field1 is also the date field
DECLARE @start DATETIME
SELECT @start = MAX(field1) FROM dbo.table
MERGE dbo.table AS TARGET
USING (SELECT field1,field2,field3,field
FROM SOURCESERVER.DATABASE..dbo
WHERE field1 >= @start) AS SOURCE
ON (TARGET.field1 = SOURCE.field1 AND TARGET.field2 = SOURCE.field2)
--when records are not matched, then insert
WHEN NOT MATCHED BY TARGET THEN
INSERT (field1,field2,field3,fiel
VALUES (SOURCE.field1,SOURCE.fiel
Business Accounts
Answer for Membership
by: aneeshattingalPosted on 2009-11-06 at 07:57:23ID: 25760227
SET the identity_insert on before the merge statement ( you need to make sure that the insert statements wont coinside)
--CREATE TABLE Products
--(
--ProductID INT identity PRIMARY KEY,
--ProductName VARCHAR(100),
--Rate MONEY
--)
--GO
--Insert records into target table
--INSERT INTO Products
--VALUES
--( 'Tea', 10.00),
--( 'Coffee', 20.00),
--( 'Muffin', 30.00),
--( 'Biscuit', 40.00)
--GO
--Create source table
CREATE TABLE UpdatedProducts
(
ProductID INT PRIMARY KEY,
ProductName VARCHAR(100),
Rate MONEY
)
GO
--Insert records into source table
INSERT INTO UpdatedProducts
VALUES
(1, 'Tea', 10.00),
(2, 'Coffee', 25.00),
(3, 'Muffin', 35.00),
(5, 'Pizza', 60.00)
GO
SELECT * FROM Products
SELECT * FROM UpdatedProducts
GO
SET IDENTITY_INSERT Products ON
--Synchronize the target table with
--refreshed data from source table
MERGE Products AS TARGET
USING UpdatedProducts AS SOURCE
ON (TARGET.ProductID = SOURCE.ProductID)
--When records are matched, update
--the records if there is any change
WHEN MATCHED AND TARGET.ProductName <> SOURCE.ProductName
OR TARGET.Rate <> SOURCE.Rate THEN
UPDATE SET TARGET.ProductName = SOURCE.ProductName,
TARGET.Rate = SOURCE.Rate
--When no records are matched, insert
--the incoming records from source
--table to target table
WHEN NOT MATCHED BY TARGET THEN
INSERT (ProductID, ProductName, Rate)
VALUES (SOURCE.ProductID, SOURCE.ProductName, SOURCE.Rate);
--When there is a row that exists in target table and
--same record does not exist in source table
--then delete this record from target table
SELECT @@ROWCOUNT;
GO
SET IDENTITY_INSERT Products OFF
GO
SELECT * FROM Products
SELECT * FROM UpdatedProducts