Controlling Flow Keywords In SQL Server

Controlling Flow Keywords In SQL Server

T-SQL includes a set of keywords that allow you to group a series of statements and make run-time decisions based on logic within the code.

  • BEGIN…END
  • BREAK
  • CONTINUE
  • GOTO
  • IF…ELSE
  • RETURN
  • WAITFOR
  • WHILE
  • TRY CATCH

These keywords use as per requirement of logic of SQL Programer to writing T-SQL.

BEGIN…END: 

Use to group or series of T-SQL Statement.  This can be nested.  Similar to “{}” in C#.net.

BEGIN
{
sql_Statement | statement_block
}
END

IF….ELSE :

can be use to put conditions on T-SQL statement.  It is possible to have  IF without ELSE but there will be no ELSE without IF.

IF Boolean_expression { sql_statement | statement_block }
[ ELSE { sql_statement | statement_block } ]

WHILE  :  

While is used for looping in T-SQL with some conditions. Till the time the condition met,T-SQL inside while loop will be executed.  There are two optional keyword can be used with While loop.

  • CONTINUE : causes the loop to restart. Any statements after the CONTINUE keyword are ignored.
  • BREAK : keyword causes the execution of the query to exit,any T-SQL code following the END keyword will be executed.

We have already explained about TRY CATCH, for more detail refer. USE OF TRY CATCH STATEMENT

WAITFOR: 

Blocks the execution of a batch, stored procedure, or transaction until a specified time or time interval is reached, or a specified statement modifies or returns at least one row.