Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
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
- Page 340 and 341: Chapter 10: Views 302 columns to a
- Page 342 and 343: Chapter 10: Views Try It Out Using
- Page 344 and 345: Chapter 10: Views 306 soh.SalesOrde
- Page 346 and 347: Chapter 10: Views AW00000676 43659
- Page 348 and 349: Chapter 10: Views NULL values will
- Page 350 and 351: Chapter 10: Views Editing V iews wi
- Page 352 and 353: Chapter 10: Views 314 There are fou
- Page 354 and 355: Chapter 10: Views Editing Views in
- Page 356 and 357: Chapter 10: Views 318 In addition,
- Page 358 and 359: Chapter 10: Views 320 from the firs
- Page 360 and 361: Chapter 10: Views You can get the y
- Page 363 and 364: 11 Writing Scripts and Batches Whet
- Page 365 and 366: Next we have a DECLARE statement to
- Page 367 and 368: I’m not going to pick any bones a
- Page 369 and 370: Using @@IDENTITY @@IDENTITY is one
- Page 371 and 372: How It Works What we’re doing in
- Page 373 and 374: DECLARE @RowCount int; --Notice the
- Page 375 and 376: When the editing tool encounters a
- Page 377 and 378: When you think about it, this seems
- Page 379 and 380: So, let’s try a quick query direc
- Page 381 and 382: We now have our text file source fo
- Page 383 and 384: Let’s build an example in the Adv
- Page 385 and 386: DECLARE @InVar varchar(50); DECLARE
- Page 387 and 388: -- This won’t work DECLARE @Numbe
- Page 389: -- Now we’re run our conditional
- Page 393 and 394: A Simple CASE A simple CASE takes a
- Page 395 and 396: 3 8 More Than One Apart 2 2 Ends Wi
- Page 397 and 398: Now, I don’t know about you, but
- Page 399 and 400: The WAITFOR statement does exactly
- Page 401 and 402: IF @ErrorNo = 2714 -- Object exists
- Page 403: Chapter 11: Writing Scripts and Bat
- Page 406 and 407: Chapter 12: Stored Procedures Creat
- Page 408 and 409: Chapter 12: Stored Procedures Dropp
- Page 410 and 411: Chapter 12: Stored Procedures Suppl
- Page 412 and 413: Chapter 12: Stored Procedures 374 [
- Page 414 and 415: Chapter 12: Stored Procedures Confi
- Page 416 and 417: Chapter 12: Stored Procedures Now,
- Page 418 and 419: Chapter 12: Stored Procedures SQL S
- Page 420 and 421: Chapter 12: Stored Procedures 382 c
- Page 422 and 423: Chapter 12: Stored Procedures It wo
- Page 424 and 425: Chapter 12: Stored Procedures 386 r
- Page 426 and 427: Chapter 12: Stored Procedures 388 -
- Page 428 and 429: Chapter 12: Stored Procedures Note
- Page 430 and 431: Chapter 12: Stored Procedures 392 n
- Page 432 and 433: Chapter 12: Stored Procedures All t
- Page 434 and 435: Chapter 12: Stored Procedures Sproc
- Page 436 and 437: Chapter 12: Stored Procedures When
- Page 438 and 439: Chapter 12: Stored Procedures 400 @
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