Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SQL Merge Question Expand / Collapse
Author
Message
Posted Wednesday, October 31, 2012 3:19 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, July 15, 2014 2:31 PM
Points: 151, Visits: 391
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

Post #1379547
Posted Wednesday, October 31, 2012 4:17 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, August 19, 2014 1:58 PM
Points: 367, Visits: 615
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.
Post #1379558
Posted Wednesday, October 31, 2012 11:46 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 6:49 PM
Points: 3,648, Visits: 5,326
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!
Post #1379630
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse