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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 11: Writing Scripts and Batches<br />

If you decide to use a temp table to communicate between scopes, just remember that any temporary<br />

table created within the scope of your EXEC statement will only live for the life of that EXEC statement.<br />

A Small Exception to the Rule<br />

There is one thing that happens inside the scope of the EXEC that can be seen after the EXEC is done —<br />

system functions — so, things like @@ROWCOUNT can still be used. Again, let’s look at a quick example:<br />

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

EXEC(‘SELECT * FROM Production.UnitMeasure’);<br />

SELECT ‘The Rowcount is ‘ + CAST(@@ROWCOUNT as varchar);<br />

This yields (after the result set):<br />

The Rowcount is 38<br />

Security Contexts and EXEC<br />

This is a tough one to cover at this point because we haven’t covered the issues yet with stored procedures<br />

and security. Still, the discussion of the EXEC command belonged here rather than in the sprocs chapter, so<br />

here we are (this is the only part of this discussion that gets wrapped up in sprocs, so bear with me).<br />

When you give someone the right to run a stored procedure, you imply that they also gain the right to<br />

perform the actions called for within the sproc. For example, let’s say we had a stored procedure that lists<br />

all the employees hired within the last year. Someone who has rights to execute the sproc can do so (and<br />

get results back) even if they do not have rights to the Employees table directly. This is really handy for<br />

reasons we will explore later in our sprocs chapter.<br />

Developers usually assume that this same implied right is valid for an EXEC statement also — it isn’t. Any<br />

reference made inside an EXEC statement will, by default, be run under the security context of the current<br />

user. So, let’s say I have the right to run a procedure called spNewEmployees, but I do not have rights to<br />

the Employees table. If spNewEmployees gets the values by running a simple SELECT statement, then<br />

everything is fine. If, however, spNewEmployees uses an EXEC statement to execute that SELECT statement,<br />

the EXEC statement will fail because I don’t have the rights to perform a SELECT on the Employees table.<br />

Since we don’t have that much information on sprocs yet, I’m going to bypass further discussion of this<br />

for now, but we will come back to it when we discuss sprocs later on.<br />

Use of Functions in Concatenation and EXEC<br />

348<br />

This behavior of a temp table only lasting the life of your EXEC procedure will show<br />

up again when we are dealing with triggers and sprocs.<br />

This one is actually more of a nuisance than anything else because there is a reasonably easy workaround.<br />

Simply put, you can’t run a function against your EXEC string in the argument for an EXEC. For example:<br />

USE AdventureWorks<strong>2008</strong>;

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

Saved successfully!

Ooh no, something went wrong!