IF...ELSE

Conditionally execute T-SQL statements.

Syntax
      IF boolean_expression
{ sql_statement | statement_block } [ ELSE { sql_statement | statement_block } ] Key boolean_expression An expression that returns TRUE or FALSE. If Boolean_expression contains a SELECT statement, it must be enclosed in parentheses. sql_statement | statement_block Any T-SQL statement or statement grouping as defined with a statement block. Unless a statement block is used, the IF or ELSE condition can affect the performance of only one T-SQL statement. Use BEGIN and END to define a statement block.

Example

IF exists (select *
from sales
where customer_id = 123)
Print 'Record exits'
ELSE
Print 'Record doesn''t exist'

“IF you can keep your head when all about you, Are losing theirs and blaming it on you...” ~ Rudyard Kipling 1899

Related:

EXISTS
Equivalent Oracle command: Select into a cursor then: IF cursorname = 123 Then...


© Copyright SS64.com 1999-2014
Some rights reserved