IF commands ... ELSE in SQL Server

Like other programming languages, SQL Server also provides users with an IF command EL .The article will detail how to use the syntax and clear examples to make it easier for you to imagine IF . ELSE.

Describe

In SQL Server, the IF statement . ELSE is used to execute conditional instructions, if the correct command executes the command, if it fails it executes another command.

Syntax of using IF . ELSE

To use IF branch statement . ELSE in SQL Server, we use the following syntax:

 IF dieukien 
{. statement executed when condition is TRUE .}

[ELSE
{. the command executes when the condition is FALSE .}]

Note:

  1. ELSE is not required.You will use the ELSE condition when you want to execute a set of statements whose IF condition is evaluated as FALSE (ie, the condition is not met).
  2. There is no ELSE IF condition in the IF . ELSE statement.Instead you must use multiple IF . nested ELSE statements to achieve the desired effect.

IF Structure Diagram . ELSE

IF commands ... ELSE in SQL Server Picture 1

Example - IF qualified statement . ELSE

 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.

Example - The statement has no ELSE conditions

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

Example - IF statements . nested ELSE

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

3.5 ★ | 2 Vote

May be interested

  • 5 Linux commands every sysadmin needs to know5 Linux commands every sysadmin needs to know
    in a world full of new tools and diverse development environments, learning some basic sysadmin commands is practically essential for any developer or engineer.
  • The difference between web server and app serverThe difference between web server and app server
    you have probably seen that the terms web server and app server are often used interchangeably as if they are related to the same thing and also facilitate the website to function properly. but in reality, they are not the same.
  • Network basics: Part 3 - DNS ServerNetwork basics: Part 3 - DNS Server
    a dns server is a server that contains a database of public ip addresses and hostnames associated with them. in most cases, the dns server is used to resolve or translate those common names into ip addresses as required.
  • 99+ entertaining AI commands99+ entertaining AI commands
    you can ask ai to create more for games you already know like monopoly, chinese chess, chess... or set up games, travel and more.
  • Extremely dangerous commands on Windows, Linux, Mac, don't try even onceExtremely dangerous commands on Windows, Linux, Mac, don't try even once
    there are commands that are used to provide certain features to the operating system. however, if used improperly, they cause danger and damage the operating system.
  • Guide to network operation for Linux users: 11 commands to knowGuide to network operation for Linux users: 11 commands to know
    linux supports commands to download files, diagnose network problems, manage network interfaces or view network statistics on the terminal. here are some common linux commands to work with, please consult.
  • 15 Tar commands should try in Linux15 Tar commands should try in Linux
    on * nix operating systems, such as linux, the commonly used utility is tar. the name of this command comes from archive tape, because it was originally designed to back up data on tape.
  • Pentnt command in WindowsPentnt command in Windows
    (applies to: windows server 2003, windows server 2003 r2, windows server 2003 with sp1, windows server 2003 with sp2).
  • How to Run Linux Commands on Windows with WSL 2How to Run Linux Commands on Windows with WSL 2
    windows subsystem for linux 2 builds on the success of the original wsl, and the newer wsl 2 brings more power and reliability to users. it is important that you understand how to use linux commands and utilities on windows subsystem for linux 2.
  • Combine all the most basic AutoCAD commandsCombine all the most basic AutoCAD commands
    using autocad shortcut commands helps to manipulate faster. if you do not know all the cad commands, please see the summary of autocad commands that we have done below.