17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

GROUP BY soh.OrderDate, sod.ProductID<br />

) AS s<br />

ON (s.ProductID = smr.ProductID)<br />

WHEN MATCHED THEN<br />

UPDATE SET smr.QtySold = smr.QtySold + s.QtySold<br />

WHEN NOT MATCHED THEN<br />

INSERT (Year, Month, ProductID, QtySold)<br />

VALUES (DATEPART(yy, s.OrderDate),<br />

DATEPART(m, s.OrderDate),<br />

s.ProductID,<br />

s.QtySold);<br />

We update the date we’re running this for (simulating running it on the second day of the month), and<br />

running it should get us 38 rows:<br />

(38 row(s) affected)<br />

But something is different this time — we already had rows in the table that our new batch of sales may have<br />

matched up with. We know we affected 38 rows, but how did we affect them? Rerun the SELECT on our table:<br />

SELECT *<br />

FROM Sales.MonthlyRollup;<br />

Chapter 7: Adding More to Our Queries<br />

And instead of 230 rows (the 192 plus the 38), we only get 194 rows. Indeed, 36 of our 38 rows were<br />

repeat sales and were therefore treated as updates, rather than insertions. Two rows (ProductIDs 882 and<br />

928) were sales of products that had not been previously sold in that month and thus needed to be<br />

inserted as new rows — one pass over the data, but the equivalent of two statements ran.<br />

We could perform similar actions that decide to delete rows based on matched or not matched conditions.<br />

A Brief Look at BY TARGET versus BY SOURCE<br />

In the examples above, we’ve largely ignored the issue of which is the table to be matched when determining<br />

the action to be performed. The default is BY TARGET, and thus all of our examples (which haven’t<br />

used the BY keyword at all) have been analyzed on whether there is or isn’t a match in the target table<br />

(the table named immediately after the MERGE keyword). The comparison, from a matching perspective, is<br />

similar to an outer join. As a join is analyzed, there can be a match on the source side, the target side, or<br />

both. If you have specified BY TARGET (or not used the BY keyword at all since matching by target is the<br />

default), the action (insert, update, or delete) is applied only if the target side of the join has a match.<br />

Likewise, if you have specified BY SOURCE, then the merge action is only applied if the source side of<br />

the join has a match.<br />

Most of the time, you can map a particular merge action to a specific match scenario:<br />

❑ NOT MATCHED [BY TARGET]: This typically maps to a scenario where you are going to be<br />

inserting rows into a table based on data you found in the source.<br />

❑ MATCHED [BY TARGET]: This implies that the row already exists in the target, and thus it is<br />

likely you will be performing an update action on the target table row.<br />

❑ NOT MATCHED BY SOURCE: This is typically utilized to deal with rows that are missing (and likely<br />

deleted) from the source table, and you will usually be deleting the row in the target under this<br />

scenario (though you may also just update the row to set an inactive flag or similar marker).<br />

209

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!