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.

And sure enough, we get back the one row we inserted earlier along with the two rows we just inserted:<br />

OrderNumber StoreCode OrderDate Quantity Terms TitleID<br />

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

TESTORDER TEST 1999-01-01 10 NET 30 1234567<br />

TESTORDER2 TST2 1999-01-01 10 NET 30 1234567<br />

TESTORDER3 TST2 1999-02-01 10 NET 30 1234567<br />

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

This new feature has the potential to really boost performance in situations where you are performing<br />

multiple INSERTs. Previously, your client application would have to issue a completely separate<br />

INSERT statement for each row of data you wanted to INSERT (there were some ways around this, but<br />

they required extra thought and effort that it seems few developers were willing to put in). Using this<br />

method can eliminate many round trips to your server; just keep in mind that it also means that your<br />

application will not be backward compatible to prior versions of <strong>SQL</strong> <strong>Server</strong>.<br />

The INSERT INTO . . . SELECT Statement<br />

What if we have a block of data that we want INSERTed? As we have just seen, we can perform multirow<br />

INSERTs explicitly, but what if we want to INSERT a block of data that can be selected from another<br />

source, such as:<br />

❑ Another table in your database<br />

❑ A totally different database on the same server<br />

❑ A heterogeneous query from another <strong>SQL</strong> <strong>Server</strong> or other data<br />

❑ The same table (usually you’re doing some sort of math or other adjustment in your SELECT<br />

statement, in this case)<br />

The INSERT INTO . . . SELECT statement can INSERT data from any of these. The syntax for this<br />

statement comes from a combination of the two statements we’ve seen thus far — the INSERT statement<br />

and the SELECT statement. It looks something like this:<br />

INSERT INTO <br />

[]<br />

<br />

Chapter 3: The Foundation Statements of T-<strong>SQL</strong><br />

The result set created from the SELECT statement becomes the data that is added in your INSERT statement.<br />

Let’s check this out by doing something that, if you get into advanced coding, you’ll find yourself doing<br />

all too often — SELECTing some data into some form of temporary table. In this case, we’re going to<br />

declare a variable of type table and fill it with rows of data from our Orders table:<br />

Like our early example database creation, this next block of code is what is called a<br />

script. Again, we will be examining batches at length in Chapter 11.<br />

73

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

Saved successfully!

Ooh no, something went wrong!