SQL Server IF ELSE
The IF statement is a part of the control flow function in SQL Server. Usually, it is a decision-making statement in various programming languages that returns a value based on the given conditions. This statement executes the code written in IF block when the given condition evaluates to true and when the condition evaluates false, then the ELSE statement will be executed.
The IF Statement
The following are the syntax that illustrates the use of this statement in SQL Server:
In the above syntax, the statement_block in the BEGIN...END block is executed when the boolean_expression is satisfied with the condition. Otherwise, this block is skipped, and the program control is moved to the statement after the END keyword. We should know that if the expression contains a SELECT statement, we need to enclose them in parentheses.
Let us see the example to understand the IF statement without ELSE block. The below example will display the result when the condition is satisfied. Otherwise, the program control moved to the statement after the END keyword, if any.
Executing the statement will give the below output:
Now, we will demonstrate it on the below 'Student' table having the following data:
The below is another example that gets the total marks of a selected student from the 'Student' table in the sample database and then prints a message if it is greater than 400.
We will get the below output:
If we want to see the above output message, we should click the Messages tab:
The IF-ELSE Statement
In the real-world scenario, we need to perform some action whenever the condition in the IF statement is TRUE or FALSE. In this case, the IF…ELSE statement is useful. This statement executes the ELSE statement block when the condition in the IF clause is evaluated FALSE.
The following are the syntax that illustrates the use of the IF ELSE statement in SQL Server:
Let us see the example to understand the IF statement with ELSE block. The below example will display the message "Congratulations! You pass the Examination" when the IF condition is satisfied. Otherwise, display "You are failed! Better Luck Next Time".
Executing the statement will give the below output. Here, the marks variable is 65, and the condition (65<45) is not satisfied. Therefore, the message inside the ELSE block is displayed:
We will get this output because the condition (65>45) is satisfied. Therefore, the message inside the IF block is displayed:
Now, we will demonstrate the IF ELSE statement on the above 'Student' table. In this example, we are going to check whether the student total marks is greater than or equal to 400 or not as follows:
Here is the program:
In this code, we have specified the @Marks variable to 600, and the condition (600 >= 550) is satisfied. Therefore, we will get the output where student records whose total marks are greater than or equal to 550 are displayed.
If we changed the @Marks variable to 500 and the condition (500 >= 550) becomes false. Therefore, we will get the output where student records whose total marks are less than 550 are displayed.
Nested IF ELSE Statement
Unlike other programming languages, we can nest an IF...ELSE statement inside another IF...ELSE statement in SQL Server. Let us demonstrate it with the following example:
In this example, we are going to check whether the age is underage, below 50, or senior citizen as follows:
Here is the result:
This article gives a complete overview of how to use the SQL Server IF ELSE statement. Here we have learned: