Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

If Else in SQL Server: IF Commands ... ELSE in SQL Server

Understand If Else in SQL Server: IF Commands ... ELSE in SQL Server with clear explanations, practical examples, and useful tips. This updated guide covers...

Table of Contents

If Else in SQL Server: IF Commands ... ELSE in SQL Server is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.

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 helps 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:

  • 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).
  • 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 Else in SQL Server: IF Commands ... ELSE in SQL Server example image 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

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 

FAQ

What should you know about describe?

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

What should you know about syntax of using IF. ELSE?

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

What should you know about 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.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.