Answers

Question and Answer:

  Home  MS SQL Server

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

"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

 148 views

More Questions for you: