SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL Merge Question


SQL Merge Question

Author
Message
haiao2000
haiao2000
Right there with Babe
Right there with Babe (742 reputation)Right there with Babe (742 reputation)Right there with Babe (742 reputation)Right there with Babe (742 reputation)Right there with Babe (742 reputation)Right there with Babe (742 reputation)Right there with Babe (742 reputation)Right there with Babe (742 reputation)

Group: General Forum Members
Points: 742 Visits: 438
Experts.
A sql merge question, from example below. Is there a way to make update @BookChangeTracking in the UPDATE condition like below (in bold)...can i do that kind of whacky stuffs or is it just too whacky to think about :-)?

DECLARE @BookInventory TABLE (TitleID INT, Title NVarchar(50), Quantity Int)
DECLARE @BookOrder TABLE (TitleID INT, Title NVarchar(50), Quantity Int)
DECLARE @BookChangeTracking TABLE (TitleID INT, OldQuantity Int, NewQuantity Int,ChangeDate DateTime)

INSERT @BookInventory
SELECT 1, 'The Catcher in the Rye', 6 UNION ALL
SELECT 2, 'Pride and Prejudice', 3 UNION ALL
SELECT 3, 'The Great Gatsby', 0 UNION ALL
SELECT 5, 'Jane Eyre', 0 UNION ALL
SELECT 6, 'Catch 22', 0 UNION ALL
SELECT 8, 'Slaughterhouse Five', 4;

INSERT @BookOrder
SELECT 1, 'The Catcher in the Rye', 3 UNION ALL
SELECT 3, 'The Great Gatsby', 0 UNION ALL
SELECT 4, 'Gone with the Wind', 4 UNION ALL
SELECT 5, 'Jane Eyre', 5 UNION ALL
SELECT 7, 'Age of Innocence', 8;

MERGE @BookInventory bi
USING @BookOrder bo ON bi.TitleID = bo.TitleID
WHEN MATCHED AND bi.Quantity + bo.Quantity = 0 THEN
DELETE
WHEN MATCHED THEN

INSERT INTO @BookChangeTracking(TitleID,OldQuantity,NewQuantity,ChangeDate)
VALUES(bi.TitleID,bi.Quantity,bi.Quantity+bo.Quantity,getdate())


UPDATE SET bi.Quantity = bi.Quantity + bo.Quantity

WHEN NOT MATCHED BY TARGET THEN
INSERT (TitleID, Title, Quantity)
VALUES (bo.TitleID, bo.Title,bo.Quantity);

select * from @BookInventory


jerry-621596
jerry-621596
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1299 Visits: 649
This will most likely error out. If I remember right, you can only have one action per MATCHED. I think the INSERT statement will be interpretted as the last part of your MERGE statement. This will result in an error on the next MATCHED.
dwain.c
dwain.c
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17505 Visits: 6431
I believe Jerry's answer is correct.

What you want to do is this:


WHEN MATCHED THEN BEGIN

INSERT INTO @BookChangeTracking(TitleID,OldQuantity,NewQuantity,ChangeDate)
VALUES(bi.TitleID,bi.Quantity,bi.Quantity+bo.Quantity,getdate())

UPDATE SET bi.Quantity = bi.Quantity + bo.Quantity
END



But the MERGE statement doesn't support BEGIN like that.

What you may be able to do is OUTPUT the results of the merge and use the $action variable available along with the INSERTED (pseudo-table) results to perform the INSERT into the @BookChangeTracking table. Now that I think about it, this may not work because you can't put a WHERE clause on what gets inserted. But you could INSERT all the results into a temp table and selectively insert into @BookChangeTracking from that in a separate statement (enclosed in a transaction of course).

Another alternative may be to use "composable DML" to do the INSERT. You should be able to Google the quoted term to read about it. This may support the WHERE clause you'd need to make it work (not sure).

Either way, I suggest you look to MS BOL for examples.

Probably the best alternative (if you can get the conditions right) is to do the INSERT in a UPDATE trigger.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
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