{. statement executed when condition is TRUE .}
[ELSE
{. the command executes when the condition is FALSE .}]
Note:
DECLARE @nhanvien_salary INT;
SET @nhanvien_salary = 15000000;
IF @nhanvien_salary> 10000000
PRINT 'Truong phong';
ELSE
PRINT 'Graduate';
GO
In the IF statement . This ELSE, if the employee salary> 12000000, the result is given as the Manager, if smaller then the Expert.
Because the ELSE is not required, this statement may be missing.
DECLARE @nhanvien_salary INT;
SET @nhanvien_salary = 15000000;
IF @nhanvien_salary <10000000
PRINT 'Graduate';
GO
In this statement, the program will give the result if the variable @nhanvien_salary
Since we cannot write ELSE IF conditions in SQL Server as in other languages, it is necessary to use multiple IF . nested ELSE statements to achieve the desired effect.
DECLARE @nhanvien_salary INT;
SET @nhanvien_salary = 15000000;
IF @nhanvien_salary> 12000000
PRINT 'Detective doc';
ELSE
BEGIN
IF @nhanvien_salary> 10000000
PRINT 'Truong phong';
ELSE
PRINT 'Graduate';
END;
GO
The example given is understood that, if the salary is greater than 12 million, the result will be the Director, otherwise this condition will continue to condition 2, if the salary is greater than 10 million the result will be the Head, Other cases are Specialists.
Previous post: PROCEDURE (Procedure) in SQL Server
Next article: WHILE loop in SQL Server