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 5: Creating and Altering Tables<br />

132<br />

The EXEC command is used in two different ways. This rendition is used to execute a stored procedure<br />

— in this case, a system stored procedure. We’ll see the second version later when we are dealing<br />

with advanced query topics and stored procedures.<br />

<strong>Tech</strong>nically speaking, you can execute a stored procedure by simply calling it (without<br />

using the EXEC keyword). The problem is that this works only if the sproc being<br />

called is the first statement of any kind in the batch. Just having sp_help Customers<br />

would have worked in the place of the previous code, but if you tried to run a SELECT<br />

statement before it — it would blow up on you. Not using EXEC leads to very unpredictable<br />

behavior and should be avoided.<br />

Try executing the command, and you’ll find that you get back several result sets one after another. The<br />

information retrieved includes separate result sets for:<br />

❑ Table name, schema, type of table (system vs. user), and creation date<br />

❑ Column names, data types, nullability, size, and collation<br />

❑ The identity column (if one exists) including the initial seed and increment values<br />

❑ The ROWGUIDCOL (if one exists)<br />

❑ Filegroup information<br />

❑ Index names (if any exist), types, and included columns<br />

❑ Constraint names (if any), types, and included columns<br />

❑ Foreign key (if any) names and columns<br />

❑ The names of any schema-bound views (more on this in Chapter 10) that depend on the table<br />

Now that we’re certain that our table was created, let’s take a look at creating yet another table — the<br />

Employees table. This time, let’s talk about what we want in the table first, and then see how you do<br />

trying to code the CREATE script for yourself.<br />

The Employees table is another fairly simple table. It should include information on:<br />

❑ The employee’s ID — this should be automatically generated by the system<br />

❑ First name<br />

❑ Optionally, middle initial<br />

❑ Last name<br />

❑ Title<br />

❑ Social Security Number<br />

❑ Salary<br />

❑ The previous salary<br />

❑ The amount of the last raise

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

Saved successfully!

Ooh no, something went wrong!