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.

Now, I don’t know about you, but I get very suspicious when I hear the word “analysis” come out of<br />

someone’s lips — particularly if that person is in a marketing or sales role. Don’t get me wrong — those<br />

people are doing their jobs just like I am. The thing is, once they ask a question one way, they usually<br />

want to ask the same question another way. That being the case, I went ahead and set this up as a<br />

script — now all we need to do when they decide they want to try it with 15 percent is make a change to<br />

the initialization value of @Markup. Let’s see what we got this time with that 10 percent markup though:<br />

ProductID Name ListPrice Marked Up Price New Price<br />

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

990 Mountain-500 Black, 42 539.99 593.989 593.9500<br />

980 Mountain-400-W Silver, 38 769.49 846.439 846.4900<br />

970 Touring-2000 Blue, 46 1214.85 1336.335 1336.4900<br />

960 Touring-3000 Blue, 62 742.35 816.585 816.7500<br />

950 ML Crankset 256.49 282.139 282.4900<br />

940 HL Road Pedal 80.99 89.089 89.4900<br />

930 HL Mountain Tire 35.00 38.50 38.7500<br />

920 LL Mountain Frame - Silver, 52 264.05 290.455 290.4900<br />

910 HL Mountain Seat/Saddle 52.64 57.904 57.9500<br />

900 LL Touring Frame - Yellow, 50 333.42 366.762 366.9500<br />

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

Look these over for a bit, and you’ll see that the results match what we were expecting. What’s more, we<br />

didn’t have to build a cursor to do it.<br />

Looping with the WHILE Statement<br />

The WHILE statement works much as it does in other languages to which you have probably been<br />

exposed. Essentially, a condition is tested each time you come to the top of the loop. If the condition is<br />

still TRUE, then the loop executes again — if not, you exit.<br />

The syntax looks like this:<br />

WHILE <br />

|<br />

[BEGIN<br />

<br />

[BREAK]<br />

| <br />

[CONTINUE]<br />

END]<br />

Chapter 11: Writing Scripts and Batches<br />

While you can just execute one statement (much as you do with an IF statement), you’ll almost never<br />

see a WHILE that isn’t followed by a BEGIN...END with a full statement block.<br />

The BREAK statement is a way of exiting the loop without waiting for the bottom of the loop to come and<br />

the expression to be re-evaluated.<br />

I’m sure I won’t be the last to tell you this, but using a BREAK is generally thought of as something of<br />

bad form in the classical sense. I tend to sit on the fence on this one. I avoid using them if reasonably<br />

possible. Most of the time, I can indeed avoid them just by moving a statement or two around while still<br />

359

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

Saved successfully!

Ooh no, something went wrong!