SQL IF –ELSE Statement:
In the SQL Server, IF –Else statement is used to execute the particular SQL statement. It is a conditional statement in which that statement is execute where condition is satisfied. If – else statement returns the Boolean expression. If the condition is true inside the IF statement then the block of statements is executed of IF Statement otherwise if the condition is not true then the block of statement is executed of Else statement.
block of statements is executed if the condition is true.
block of statement is executed if the condition is false.
Note - you can remove the ELSE statement from IF condition because ELSE is optional and it execute when the condition inside the IF statement is false.
If you want to execute multiple statements inside the IF and ELSE statement then you need to use BEGIN and END inside the IF and ELSE statements.
We can also used SQL command in IF ELSE statement to execute the record as needs
Lets have a database table ‘Employees’
The following SQL IF- ELSE statement is used to find the sum of salary of Employees from ‘Employees’ table. If the salary is more than 30000 then it returns the name AS Engineers and if salary is less than 30000 then it returns the name AS Training.
DECLARE @value INT;
select @value=SUM(Salery) from Employees
select EmpName AS Engineer,Salery from Employees;
select EmpName AS Trainning, Salery from Employees;