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.

DECLARE @InVar varchar(50);<br />

DECLARE @OutVar varchar(50);<br />

-- Set up our string to feed into the EXEC command<br />

SET @InVar = ‘SELECT @OutVar = FirstName FROM Person.Contact WHERE ContactID = 1’;<br />

-- Now run it<br />

EXEC (@Invar);<br />

-- Now, just to show there’s no difference, run the select without using a in variable<br />

EXEC (‘SELECT @OutVar = FirstName FROM Person.Contac WHERE ContactID = 1’);<br />

-- @OutVar will still be NULL because we haven’t been able to put anything in it<br />

SELECT @OutVar;<br />

Now, look at the output from this:<br />

Msg 137, Level 15, State 1, Line 1<br />

Must declare the scalar variable “@OutVar”.<br />

Msg 137, Level 15, State 1, Line 1<br />

Must declare the scalar variable “@OutVar”.<br />

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

NULL<br />

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

<strong>SQL</strong> <strong>Server</strong> wastes no time in telling us that we are scoundrels and clearly don’t know what we’re doing.<br />

Why do we get a Must Declare error message when we have already declared @OutVar? Because we’ve<br />

declared it in the outer scope — not within the EXEC itself.<br />

Let’s look at what happens if we run things a little differently:<br />

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

-- This time, we only need one variable. It does need to be longer though.<br />

DECLARE @InVar varchar(200);<br />

/* Set up our string to feed into the EXEC command. This time we’re going<br />

** to feed it several statements at a time. They will all execute as one<br />

** batch.<br />

*/<br />

SET @InVar = ‘DECLARE @OutVar varchar(50);<br />

SELECT @OutVar = FirstName FROM Person.Person WHERE BusinessEntityID = 1;<br />

SELECT ‘’The Value Is ‘’ + @OutVar’;<br />

-- Now run it<br />

EXEC (@Invar);<br />

This time we get back results closer to what we expect:<br />

Chapter 11: Writing Scripts and Batches<br />

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

The Value Is Ken<br />

Notice the way that I’m using two quote marks right next to each other to indicate that I really want a<br />

quote mark rather than to terminate my string.<br />

So, what we’ve seen here is that we have two different scopes operating, and nary the two shall meet.<br />

There is, unfortunately, no way to pass information between the inside and outside scopes without using<br />

an external mechanism such as a temporary table or a special stored procedure called sp_executesql.<br />

347

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

Saved successfully!

Ooh no, something went wrong!