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 11: Writing Scripts and Batches<br />

If your table names went into the Dynamic<strong>SQL</strong>Example table the way mine did, then a TableID of 25<br />

should equate to the Production.UnitMeasure table. If so, you should wind up with something like<br />

this (the rightmost columns have been snipped for brevity):<br />

UnitMeasureCode Name ModifiedDate<br />

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

BOX Boxes 1998-06-01 00:00:00.000<br />

BTL Bottle 1998-06-01 00:00:00.000<br />

C Celsius 1998-06-01 00:00:00.000<br />

…<br />

…<br />

PC Piece 1998-06-01 00:00:00.000<br />

PCT Percentage 1998-06-01 00:00:00.000<br />

PT Pint, US liquid 1998-06-01 00:00:00.000<br />

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

The Gotchas of EXEC<br />

Like most things that are of interest, using EXEC is not without its little trials and tribulations. Among<br />

the gotchas of EXEC are:<br />

❑ It runs under a separate scope than the code that calls it — that is, the calling code can’t reference<br />

variables inside the EXEC statement, and the EXEC can’t reference variables in the calling<br />

code after they are resolved into the string for the EXEC statement. If you need to pass values<br />

between your dynamic <strong>SQL</strong> and the routine that calls it, consider using sp_executesql.<br />

❑ By default, it runs under the same security context as the current user — not that of the calling<br />

object (an object generally runs under the context of the object’s owner, not the current user).<br />

❑ It runs under the same connection and transaction context as the calling object (we’ll discuss<br />

this further in Chapter 14).<br />

❑ Concatenation that requires a function call must be performed on the EXEC string prior to actually<br />

calling the EXEC statement — you can’t do the concatenation of function in the same statement<br />

as the EXEC call.<br />

❑ EXEC cannot be used inside a user-defined function.<br />

Each of these can be a little difficult to grasp, so let’s look at each individually.<br />

The Scope of EXEC<br />

346<br />

Determining variable scope with the EXEC statement is something less than intuitive. The actual statement<br />

line that calls the EXEC statement has the same scope as the rest of the batch or procedure that the<br />

EXEC statement is running in, but the code that is performed as a result of the EXEC statement is considered<br />

to be in its own batch. As is so often the case, this is best shown with an example:<br />

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

/* First, we’ll declare to variables. One for stuff we’re putting into<br />

** the EXEC, and one that we think will get something back out (it won’t)<br />

*/

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

Saved successfully!

Ooh no, something went wrong!