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 3: The Foundation Statements of T-<strong>SQL</strong><br />

72<br />

Which returns (among other things):<br />

Column_name Type Computed Length Prec Scale Nullable<br />

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

OrderNumber varchar no 20 no<br />

StoreCode char no 4 no<br />

OrderDate date no 3 10 0 no<br />

Quantity int no 4 10 0 no<br />

Terms varchar no 12 no<br />

TitleID int no 4 10 0 no<br />

The Sales table has six columns in it, but pay particular attention to the Quantity, OrderDate, and<br />

TitleID columns; they are of types that we haven’t done INSERTs with up to this point.<br />

What you need to pay attention to in this query is how to format the types as you’re inserting them. We<br />

do not use quotes for numeric values as we have with our character data. However, the date data type<br />

does require quotes. (Essentially, it goes in as a string and it then gets converted to a date.)<br />

INSERT INTO Sales<br />

(StoreCode, OrderNumber, OrderDate, Quantity, Terms, TitleID)<br />

VALUES<br />

(‘TEST’, ‘TESTORDER’, ‘01/01/1999’, 10, ‘NET 30’, 1234567);<br />

This gets back the now familiar (1 row(s) affected) message. Notice, however, that I moved around<br />

the column order in my insert. The data in the VALUES portion of the insert needs to match the column<br />

list, but the column list can be in any order you choose to make it; it does not have to be in the order the<br />

columns are listed in the physical table.<br />

Note that while I’ve used the MM/DD/YYYY format that is popular in the U.S., you<br />

can use a wide variety of other formats (such as the internationally more popular<br />

YYYY-MM-DD) with equal success. The default for your server will vary depending<br />

on whether you purchase a localized copy of <strong>SQL</strong> <strong>Server</strong> or if the setting has been<br />

changed on the server.<br />

Multirow Inserts<br />

New with <strong>SQL</strong> <strong>Server</strong> <strong>2008</strong> is the ability to INSERT multiple rows at one time. To do this, just keep tacking<br />

on additional comma-delimited insertion values, for example:<br />

INSERT INTO Sales<br />

(StoreCode, OrderNumber, OrderDate, Quantity, Terms, TitleID)<br />

VALUES<br />

(‘TST2’, ‘TESTORDER2’, ‘01/01/1999’, 10, ‘NET 30’, 1234567),<br />

(‘TST2’, ‘TESTORDER3’, ‘02/01/1999’, 10, ‘NET 30’, 1234567);<br />

This inserts both sets of values using a single statement. To check what we got, let’s go ahead and look in<br />

the Sales table:<br />

SELECT *<br />

FROM Sales;

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

Saved successfully!

Ooh no, something went wrong!