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.

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

Which is quite a bit different from what CAST did. Indeed, you could have converted to any one of 34<br />

two-digit- or four-digit-year formats.<br />

SELECT OrderDate, CONVERT(varchar(12), OrderDate, 5) AS Converted<br />

FROM Sales.SalesOrderHeader<br />

WHERE SalesOrderID = 43663;<br />

This returns:<br />

OrderDate Converted<br />

----------------------- ------------<br />

2001-07-01 00:00:00.000 01-07-01<br />

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

All you need is to supply a code at the end of the CONVERT function (111 in the preceding example gave<br />

us the Japan standard, with a four-digit year, and 5 the Italian standard, with a two-digit year) that tells<br />

which format you want. Anything in the 100s is a four-digit year; anything less than 100, with a few<br />

exceptions, is a two-digit year. The available formats can be found in Books Online under the topic of<br />

CONVERT or CASE.<br />

Keep in mind that you can set a split point that <strong>SQL</strong> <strong>Server</strong> will use to determine whether a two-digit<br />

year should have a 20 added on the front or a 19. The default breaking point is 49/50 — a two-digit year<br />

of 49 or less will be converted using a 20 on the front. Anything higher will use a 19. These can be changed<br />

in the database server configuration (administrative issues are discussed in Chapter 19).<br />

The MERGE Command<br />

206<br />

The MERGE command is new with <strong>SQL</strong> <strong>Server</strong> <strong>2008</strong> and provides a somewhat different way of thinking<br />

about DML statements. With MERGE, we have the prospect of combining multiple DML action statements<br />

(INSERT, UPDATE, DELETE) into one overall action, improving performance (they can share many of the<br />

same physical operations) and simplifying transactions. MERGE makes use of a special USING clause that<br />

winds up working somewhat like a CTE. The result set in the USING clause can then be used to conditionally<br />

apply your INSERT, UPDATE, and DELETE statements. The basic syntax looks something like this:<br />

MERGE [ TOP ( ) [ PERCENT ] ]<br />

[ INTO ] [ WITH ( ) ] [ [ AS ] ]<br />

USING <br />

ON <br />

[ WHEN MATCHED [ AND ]<br />

THEN ]<br />

[ WHEN NOT MATCHED [ BY TARGET ] [ AND ]<br />

THEN ]<br />

[ WHEN NOT MATCHED BY SOURCE [ AND ]<br />

THEN ]<br />

[ ]<br />

[ OPTION ( [ ,...n ] ) ];<br />

Let’s use the example of receiving a shipment for inventory. We’ll assume that we’re keeping a special<br />

roll up table of our sales for reporting purposes. We want to run a daily query that will add any new<br />

sales to our monthly rollup. On the first night of the month, this is pretty much a no-brainer, as, since

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

Saved successfully!

Ooh no, something went wrong!