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 7: Adding More to Our Queries<br />

that it knows to return FALSE for that lookup rather than TRUE. Performance-wise, everything else about<br />

the query is the same.<br />

Using EXISTS in Other Ways<br />

202<br />

One common use of EXISTS is to check for the existence of a table before running a CREATE statement.<br />

You may want to drop an existing table, or you just may way to change to an ALTER statement or some<br />

other statement that adjusts the existing table if there is one. One of the most common ways you’ll see<br />

this done will look something like this:<br />

IF EXISTS<br />

(SELECT *<br />

FROM sys.objects<br />

WHERE OBJECT_NAME(object_id) = 'foo'<br />

AND SCHEMA_NAME(schema_id) = 'dbo'<br />

AND OBJECTPROPERTY(object_id, 'IsUserTable') = 1)<br />

BEGIN<br />

DROP TABLE dbo.foo;<br />

PRINT 'Table foo has been dropped';<br />

END<br />

GO<br />

CREATE TABLE dbo.foo<br />

(<br />

Column1 int IDENTITY(1,1) NOT NULL,<br />

Column2 varchar(50)NULL<br />

);<br />

Since EXISTS returns nothing but TRUE or FALSE, that means it works as an excellent conditional expression.<br />

The preceding example will run the DROP TABLE code only if the table exists; otherwise, it skips over<br />

that part and moves right into the CREATE statement. This avoids one of two errors showing up when you<br />

run the script. First, that it can’t run the CREATE statement (which would probably create other problems<br />

if you were running this in a script where other tables were depending on this being done first) because<br />

the object already exists. Second, that it couldn’t DROP the table (this pretty much just creates a message<br />

that might be confusing to a customer who installs your product) because it didn’t exist. You’re covered<br />

for both.<br />

As an example of this, let’s write our own CREATE script for something that’s often skipped in the automation<br />

effort — the database. But creation of the database is often left as part of some cryptic directions that<br />

say something like “create a database called ‘xxxx’.” The fun part is when the people who are actually<br />

installing it (who often don’t know what they’re doing) start including the quotes, or create a database<br />

that is too small, or a host of other possible and very simple errors to make. This is the point where I hope<br />

you have a good tech support department.<br />

Instead, we’ll just build a little script to create the database object that could go with AdventureWorks<strong>2008</strong>.<br />

For safety’s sake, we’ll call it AdventureWorksCreate. We’ll also keep the statement to a minimum because<br />

we’re interested in the EXISTS rather than the CREATE command:<br />

USE MASTER;<br />

GO

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

Saved successfully!

Ooh no, something went wrong!