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.

We now have our text file source for our query, and are nearly ready to have sqlcmd help us generate<br />

our output. First, however, is that we need there to be some data from yesterday (none of the sample data<br />

is going to have data from yesterday unless you just ran the UPDATE statement we used in Chapter 10 to<br />

change to orders to yesterday’s date. Just to be sure of which update I’m talking about here, I mean the<br />

one shown in the views chapter. So, with this in mind, let’s run that statement one more time (you can<br />

do this through the Query window if you like):<br />

USE AdventureWorks<strong>2008</strong><br />

UPDATE Sales.SalesOrderHeader<br />

SET OrderDate = CAST(DATEADD(day,-1,GETDATE()) AS Date),<br />

DueDate = CAST(DATEADD(day,11,GETDATE()) AS Date),<br />

ShipDate = CAST(DATEADD(day,6,GETDATE()) AS Date)<br />

WHERE SalesOrderID < 43663;<br />

OK, so we have at least one row that is an order with yesterday’s date now. So we’re most of the way<br />

ready to go; however, we’ve said we want our results to a text file, so we’ll need to add some extra<br />

parameters to our sqlcmd command line this time to tell <strong>SQL</strong> <strong>Server</strong> where to put the output:<br />

C:\>sqlcmd -UMyLogin -PMyPass -iYesterdaysOrders.sql -oYesterdaysOrders.txt<br />

There won’t be anything special or any fanfare when sqlcmd is done running this — you’ll simply get your<br />

Windows drive prompt again (C:\ most likely), but check out what is in our YesterdaysOrders.txt<br />

file now:<br />

C:\>TYPE YesterdaysOrders.txt<br />

This gives us our one row:<br />

Chapter 11: Writing Scripts and Batches<br />

C:\>TYPE YesterdaysOrders.txt<br />

Changed database context to ‘AdventureWorks<strong>2008</strong>’.<br />

AccountNumber SalesOrderID OrderDate ProductID Name<br />

OrderQty UnitPrice TotalDiscount<br />

LineTotal<br />

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

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

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

AW00000676 43659 2007-10-06 00:00:00.000 776 Mountain-100 Blac<br />

k, 42 1 2024.9940<br />

0.0000 2024.994000<br />

AW00000676 43659 2007-10-06 00:00:00.000 777 Mountain-100 Blac<br />

k, 44 3 2024.9940<br />

0.0000 6074.982000<br />

AW00000676 43659 2007-10-06 00:00:00.000 778 Mountain-100 Blac<br />

k, 48 1 2024.9940<br />

0.0000 2024.994000<br />

…<br />

…<br />

AW00000227 43662 2007-10-06 00:00:00.000 738 LL Road Frame - B<br />

lack, 52 1 178.5808<br />

0.0000 178.580800<br />

AW00000227 43662 2007-10-06 00:00:00.000 766 Road-650 Black, 6<br />

0 3 419.4589<br />

0.0000 1258.376700<br />

343

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

Saved successfully!

Ooh no, something went wrong!