Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

cdn.s3techtraining.com
from cdn.s3techtraining.com More from this publisher
17.06.2013 Views

Chapter 11: Writing Scripts and Batches To expand our earlier example just a bit, let’s actually print a warning message out if we do not create our table: -- Now we’re run our conditional CREATE statement IF NOT EXISTS ( SELECT s.name AS SchemaName, t.name AS TableName FROM sys.schemas s JOIN sys.tables t ON s.schema_id = t.schema_id WHERE s.name = ‘dbo’ AND t.name = ‘OurIFTest’ ) CREATE TABLE OurIFTest( Col1 int PRIMARY KEY ); ELSE PRINT ‘WARNING: Skipping CREATE as table already exists’; If you have already run the preceding example, then the table will already exist, and running this second example should get you the warning message: WARNING: Skipping CREATE as table already exists Grouping Code into Blocks 352 Sometimes you need to treat a group of statements as though they were all one statement (if you execute one, then you execute them all — otherwise, you don’t execute any of them). For instance, the IF statement will, by default, consider only the very next statement after the IF to be part of the conditional code. What if you want the condition to require several statements to run? Life would be pretty miserable if you had to create a separate IF statement for each line of code you wanted to run if the condition holds. Thankfully, like most any language with an IF statement, SQL Server gives us a way to group code into blocks that are considered to all belong together. The block is started when you issue a BEGIN statement and continues until you issue an END statement. It works like this: IF BEGIN --First block of code starts here -- executes only if --expression is TRUE Statement that executes if expression is TRUE Additional statements ... ... Still going with statements from TRUE expression IF --Only executes if this block is active BEGIN Statement that executes if both outside and inside expressions are TRUE Additional statements ... ... Still statements from both TRUE expressions END

Out of the condition from inner condition, but still part of first block END --First block of code ends here ELSE BEGIN Statement that executes if expression is FALSE Additional statements ... ... Still going with statements from FALSE expression END Chapter 11: Writing Scripts and Batches Notice our ability to nest blocks of code. In each case, the inner blocks are considered to be part of the outer block of code. I have never heard of there being a limit to how many levels deep you can nest your BEGIN...END blocks, but I would suggest that you minimize them. There are definitely practical limits to how deep you can keep them readable — even if you are particularly careful about the formatting of your code. Just to put this notion into play, let’s make yet another modification to table creation. This time, we’re going to provide an informational message regardless of whether the table was created or not. -- This time we’re adding a check to see if the table DOES already exist -- We’ll remove it if it does so that the rest of our example can test the -- IF condition. Just remove this first IF EXISTS block if you want to test -- the ELSE condition below again. IF EXISTS ( SELECT s.name AS SchemaName, t.name AS TableName FROM sys.schemas s JOIN sys.tables t ON s.schema_id = t.schema_id WHERE s.name = ‘dbo’ AND t.name = ‘OurIFTest’ ) DROP TABLE OurIFTest; -- Now we’re run our conditional CREATE statement IF NOT EXISTS ( SELECT s.name AS SchemaName, t.name AS TableName FROM sys.schemas s JOIN sys.tables t ON s.schema_id = t.schema_id WHERE s.name = ‘dbo’ AND t.name = ‘OurIFTest’ ) BEGIN PRINT ‘Table dbo.OurIFTest not found.’; PRINT ‘CREATING: Table dbo.OurIFTest’; CREATE TABLE OurIFTest( Col1 int PRIMARY KEY ); END ELSE PRINT ‘WARNING: Skipping CREATE as table already exists’; 353

Out of the condition from inner condition, but still<br />

part of first block<br />

END --First block of code ends here<br />

ELSE<br />

BEGIN<br />

Statement that executes if expression is FALSE<br />

Additional statements<br />

...<br />

...<br />

Still going with statements from FALSE expression<br />

END<br />

Chapter 11: Writing Scripts and Batches<br />

Notice our ability to nest blocks of code. In each case, the inner blocks are considered to be part of the<br />

outer block of code. I have never heard of there being a limit to how many levels deep you can nest your<br />

BEGIN...END blocks, but I would suggest that you minimize them. There are definitely practical limits<br />

to how deep you can keep them readable — even if you are particularly careful about the formatting of<br />

your code.<br />

Just to put this notion into play, let’s make yet another modification to table creation. This time, we’re<br />

going to provide an informational message regardless of whether the table was created or not.<br />

-- This time we’re adding a check to see if the table DOES already exist<br />

-- We’ll remove it if it does so that the rest of our example can test the<br />

-- IF condition. Just remove this first IF EXISTS block if you want to test<br />

-- the ELSE condition below again.<br />

IF EXISTS (<br />

SELECT s.name AS SchemaName, t.name AS TableName<br />

FROM sys.schemas s<br />

JOIN sys.tables t<br />

ON s.schema_id = t.schema_id<br />

WHERE s.name = ‘dbo’<br />

AND t.name = ‘OurIFTest’<br />

)<br />

DROP TABLE OurIFTest;<br />

-- Now we’re run our conditional CREATE statement<br />

IF NOT EXISTS (<br />

SELECT s.name AS SchemaName, t.name AS TableName<br />

FROM sys.schemas s<br />

JOIN sys.tables t<br />

ON s.schema_id = t.schema_id<br />

WHERE s.name = ‘dbo’<br />

AND t.name = ‘OurIFTest’<br />

)<br />

BEGIN<br />

PRINT ‘Table dbo.OurIFTest not found.’;<br />

PRINT ‘CREATING: Table dbo.OurIFTest’;<br />

CREATE TABLE OurIFTest(<br />

Col1 int PRIMARY KEY<br />

);<br />

END<br />

ELSE<br />

PRINT ‘WARNING: Skipping CREATE as table already exists’;<br />

353

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

Saved successfully!

Ooh no, something went wrong!