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

76<br />

Which returns the following:<br />

StoreCode Name Address City State Zip<br />

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

TEST Test Store 1234 Anywhere Street Here NY 00319<br />

Let’s update the value in the City column:<br />

UPDATE Stores<br />

SET City = ‘There’<br />

WHERE StoreCode = ‘TEST’;<br />

Much like when we ran the INSERT statement, we don’t get much back from <strong>SQL</strong> <strong>Server</strong>:<br />

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

Yet when we run our SELECT statement again, we see that the value has indeed changed:<br />

StoreCode Name Address City State Zip<br />

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

TEST Test Store 1234 Anywhere Street There NY 00319<br />

Note that we could have changed more than one column just by adding a comma and the additional<br />

column expression. For example, the following statement would have updated both columns:<br />

UPDATE Stores<br />

SET city = ‘There’, state = ‘NY’<br />

WHERE StoreCode = ‘TEST’;<br />

If we choose, we can use an expression for the SET clause instead of the explicit values we’ve used thus<br />

far. For example, we could add a suffix on all of the store names by concatenating the existing store<br />

name with a suffix:<br />

UPDATE Stores<br />

SET Name = Name + ‘ - ‘ + StoreCode;<br />

After executing that UPDATE, run a SELECT statement on Stores:<br />

SELECT *<br />

FROM Stores<br />

You should see the Name suffixed by the StoreCode:<br />

StoreCode Name Address City State Zip<br />

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

TEST Test Store - TEST 1234 Anywhere Street There NY 00319<br />

TST2 Test Store - TST2 NULL Here NY 00319<br />

As you can see, a single UPDATE statement can be fairly powerful. Even so, this is really just the beginning.<br />

We’ll see even more advanced updates in later chapters.

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

Saved successfully!

Ooh no, something went wrong!