SQL
What is SQL? Learn SQL Basics, SQL Full Form & How to Use
What is SQL? SQL is the standard language for dealing with Relational Databases. SQL can be used...
In real life, you perform many actions which are dependent on the outcome of some other activity or situation.
Some real-time examples are:
Here, you can see that one action, like Road trip above, is conditionally dependent on the outcome of another activity which is "whether it will rain or not tomorrow!"
Similarly, MS SQL also provides the capability to execute T-SQL statement conditionally.
In this tutorial, you will learn:
In MS SQL, IF…ELSE is a type of Conditional statement.
Any T-SQL statement can be executed conditionally using IF… ELSE.
Below figure explain IF…ELSE statement
Syntax:
IF <Condition>
{Statement | Block_of_statement}
[ ELSE
{Statement | Block_of_statement}]
Rules:
Condition: TRUE
IF (1=1) PRINT 'IF STATEMENT: CONDITION IS TRUE' ELSE PRINT 'ELSE STATEMENT: CONDITION IS FALSE'
Condition: FLASE
IF (1=2) PRINT 'IF STATEMENT: CONDITION IS TRUE' ELSE PRINT 'ELSE STATEMENT: CONDITION IS FALSE'
Assumption: Assume that you have the table as 'gtupapers' with two columns and four rows as displayed below:
We will use 'gtupapers' table in further examples
Condition: TRUE
DECLARE @Course_ID INT = 4 IF (@Course_ID = 4) Select * from gtupapers where Tutorial_ID = 4 ELSE Select * from gtupapers where Tutorial_ID != 4
Condition: FLASE
DECLARE @Course_ID INT = 4 IF (@Course_ID != 4) Select * from gtupapers where Tutorial_ID = 4 ELSE Select * from gtupapers where Tutorial_ID != 4
Condition: TRUE
DECLARE @Course_ID INT = 2 IF (@Course_ID <=2) BEGIN Select * from gtupapers where Tutorial_ID = 1 Select * from gtupapers where Tutorial_ID = 2 END ELSE BEGIN Select * from gtupapers where Tutorial_ID = 3 Select * from gtupapers where Tutorial_ID = 4 END
Condition: FALSE
DECLARE @Course_ID INT = 2 IF (@Course_ID >=3) BEGIN Select * from gtupapers where Tutorial_ID = 1 Select * from gtupapers where Tutorial_ID = 2 END ELSE BEGIN Select * from gtupapers where Tutorial_ID = 3 Select * from gtupapers where Tutorial_ID = 4 END
You can use an IF statement without an ELSE part. Remember we stated that the ELSE part is optional. For example:
DECLARE @Course_ID INT = 2 IF (@Course_ID <=2) Select * from gtupapers where Tutorial_ID = 1
It prints the following:
Executing the false condition will give no output. Consider the following query
DECLARE @Course_ID INT = 2 IF (@Course_ID <=0) Select * from gtupapers where Tutorial_ID = 1
The result it
Unlike other programming languages, you cannot add an ELSE IF statement within an IF…ELSE statement. This is why you can nest IF…ELSE statements. It is demonstrated below:
DECLARE @age INT;
SET @age = 60;
IF @age < 18
PRINT 'underage';
ELSE
BEGIN
IF @age < 50
PRINT 'You are below 50';
ELSE
PRINT 'Senior';
END;
What is SQL? SQL is the standard language for dealing with Relational Databases. SQL can be used...
In this SQL Query cheat sheet you will learn {loadposition table-of-content} What You Will Learn:...
WHAT IS THE ALTER COMMAND? As the saying goes Change is the only constant With time business...
SQLite supports different types of SQL Joins, like INNER JOIN, LEFT OUTER JOIN, and CROSS JOIN....
What is Exception Handling in PL/SQL? An exception occurs when the PL/SQL engine encounters an...
What are TCL Statements in PL/SQL? TCL stands for Transaction Control Statements. It will either save...