Home >> FAQs/Tutorials >> SQL Server FAQ
SQL Server FAQ - "IF ... ELSE IF ... ELSE ..." Statement Structures
(Continued from previous topic...)
How To Use "IF ... ELSE IF ... ELSE ..." Statement Structures?
"IF ... ELSE IF ... ELSE ..." statement structure is used to select one of the specified statements to be executed
based on pecified Boolean conditions. Here is the syntax of "IF ... ELSE IF ... ELSE ..." statement structure:
IF condition_1 statement_1;
ELSE IF condition_2 statement_2;
ELSE IF condition_n statement_n;
-- Executes statement_x is
if condition_x results in Boolean TRUE
The tutorial exercise below shows you how to use an IF ... ELSE statement structure to selectively
execute one of the CREATE TABLE statements:
DECLARE @site_name VARCHAR(40);
SET @site_name = 'SQA';
IF @site_name = 'DBA'
CREATE TABLE dba_links (url VARCHAR(256));
ELSE IF @site_name = 'SQA'
CREATE TABLE sqa_links (url VARCHAR(256));
PRINT 'Unknown site name: '+@site_name;
Command(s) completed successfully.
SELECT name FROM sys.tables WHERE name LIKE '%links';
(Continued on next topic...)
- How To Use "IF ... ELSE IF ... ELSE ..." Statement Structures?
- How To Use "BEGIN ... END" Statement Structures?
- How To Use WHILE Loops?
- How To Stop a Loop Early with BREAK Statements?
- How To Skip Remaining Statements in a Loop Block Using CONTINUE Statements?