Keywords for flow control in Transact-SQL include BEGIN and END, BREAK, CONTINUE, GOTO, IF and ELSE, RETURN, WAITFOR, and WHILE. IF and ELSE allow conditional execution. This batch statement will print "It is the weekend" if the current date is a weekend day, or "It is a weekday" if the current date is a weekday. (Note: This code assumes that Sunday is configured as the first day of the week in the @@DATEFIRST setting.) IF DATEPART(dw, GETDATE()) = 7 OR DATEPART(dw, GETDATE()) = 1 PRINT 'It is the weekend.'; ELSE PRINT 'It is a weekday.'; BEGIN and END mark a
block of statements. If more than one statement is to be controlled by the conditional in the example above, we can use BEGIN and END like this: IF DATEPART(dw, GETDATE()) = 7 OR DATEPART(dw, GETDATE()) = 1 BEGIN PRINT 'It is the weekend.'; PRINT 'Get some rest on the weekend!'; END; ELSE BEGIN PRINT 'It is a weekday.'; PRINT 'Get to work on a weekday!'; END; WAITFOR will wait for a given amount of time, or until a particular time of day. The statement can be used for delays or to block execution until the set time. RETURN is used to immediately return from a
stored procedure or function. BREAK ends the enclosing WHILE loop, while CONTINUE causes the next iteration of the loop to execute. An example of a WHILE loop is given below. DECLARE @i INT; SET @i = 0; WHILE @i ==Changes to DELETE and UPDATE statements==