MS SQL Server Concepts and Programming Question:
Download Questions PDF

How To Use "IF ... ELSE IF ... ELSE ..." Statement Structures in MS SQL Server?

Answer:

"IF ... ELSE IF ... ELSE ..." statement structure is used to select one of the specified statements to be executed based on pacified 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;
ELSE statement_o;
-- 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:

USE GlobalGuideLineDatabase
GO

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));
ELSE
PRINT 'Unknown site name: '+@site_name;
GO
Command(s) completed successfully.

SELECT name FROM sys.tables WHERE name LIKE '%links';
GO
sqa_links


Download MS SQL Server Interview Questions And Answers PDF

Previous QuestionNext Question
What Are Logical/Boolean Operations in MS SQL Server?How To Use "BEGIN ... END" Statement Structures in MS SQL Server?