How To Use "IF ... ELSE IF ... ELSE ..." Statement Structures in MS SQL Server?
Submitted by: Administrator"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
Submitted by: Administrator
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
Submitted by: Administrator
Read Online MS SQL Server Job Interview Questions And Answers
Top MS SQL Server Questions
☺ | How To Sort Query Output in Descending Order in MS SQL Server? |
☺ | How to download and install the scaled-down database AdventureWorksLT? |
☺ | How To Find Out What Is the Default Collation in a Database? |
☺ | How To Use Subqueries with the IN Operators in MS SQL Server? |
☺ | How To Create a Testing Table with Test Data in MS SQL Server? |
Top Databases Programming Categories
☺ | RDBMS Interview Questions. |
☺ | SQL Interview Questions. |
☺ | SSRS Interview Questions. |
☺ | Sybase Interview Questions. |
☺ | Database Administrator (DBA) Interview Questions. |