logo

SQL Server IF ELSE

IF 문은 SQL Server의 제어 흐름 기능의 일부입니다. 일반적으로 다양한 프로그래밍 언어로 된 의사결정문입니다. 주어진 조건에 따라 값을 반환합니다. . 이 문은 주어진 조건이 true로 평가되면 IF 블록에 작성된 코드를 실행하고 조건이 false로 평가되면 ELSE 문이 실행됩니다.

IF 문

다음은 SQL Server에서 이 문의 사용을 보여 주는 구문입니다.

 IF boolean_expression BEGIN { statement_block } END 

위 구문에서, 성명_블록 에서 시작...끝 블록은 다음과 같은 경우 실행됩니다. 부울 식 조건에 만족합니다. 그렇지 않으면 이 블록을 건너뛰고 프로그램 제어는 다음 명령문으로 이동됩니다. 예어. 우리는 표현식에 다음이 포함되어 있는지 알아야 합니다. 선택하다 성명서, 우리는해야합니다 괄호로 묶으세요. .

마두리가 말했다.

ELSE 블록이 없는 IF 문을 이해하기 위해 예를 살펴보겠습니다. 아래 예에서는 조건이 만족되면 결과를 표시합니다. 그렇지 않으면 프로그램 제어가 END 키워드 뒤의 명령문으로 이동합니다(있는 경우).

 DECLARE @Marks INT = 65 ; IF @marks >= 45 BEGIN PRINT 'Congratulations! You pass the Examination'; END 

명령문을 실행하면 아래 출력이 제공됩니다.

운영 체제 예
SQL Server IF ELSE

이제 아래 '에서 시연하겠습니다. 학생' 다음 데이터가 있는 테이블:

SQL Server IF ELSE

아래는 총점 '에서 선발된 학생의 학생' 샘플 데이터베이스의 테이블을 복사한 다음 메시지 그렇다면 400보다 큼 .

 BEGIN DECLARE @Total_Marks INT; SELECT @Total_Marks = total_marks FROM Student WHERE age>25; SELECT @Total_Marks; IF @Total_Marks > 400 BEGIN PRINT 'Congratulations! You pass the Examination'; END END 

우리는 아래와 같은 결과를 얻을 것입니다:

SQL Server IF ELSE

위의 출력 메시지를 보려면 메시지 탭:

SQL Server IF ELSE

IF-ELSE 문

실제 시나리오에서는 IF 문의 조건이 TRUE 또는 FALSE일 때마다 특정 작업을 수행해야 합니다. 이 경우 IF…ELSE 문이 유용합니다. 이 문은 IF 절의 조건이 FALSE로 평가될 때 ELSE 문 블록을 실행합니다.

다음은 SQL Server에서 IF ELSE 문의 사용을 보여 주는 구문입니다. :

jsp 자바 포인트
 IF expression BEGIN Statement block -- It executes when the IF clause expression is TRUE. END ELSE BEGIN Statement block -- It executes when the IF clause expression is FALSE. END 

ELSE 블록이 포함된 IF 문을 이해하기 위해 예를 살펴보겠습니다. 아래 예에서는 '라는 메시지가 표시됩니다. 축하해요! 당신은 시험에 합격 ' IF 조건을 만족하는 경우. 그렇지 않으면 '를 표시합니다. 당신은 실패했습니다! 다음 기회에 '.

 DECLARE @Marks INT; SET @Marks = 65; IF @marks <45 begin print 'congratulations! you pass the examination'; end else 'you are failed! better luck next time'; < pre> <p>Executing the statement will give the below output. Here, the <strong>marks</strong> variable is <strong>65</strong> , and the <strong>condition (65<45)< strong> is not satisfied. Therefore, the message inside the ELSE block is displayed:</45)<></strong></p> <img src="//techcodeview.com/img/sql-server-tutorials/49/sql-server-if-else-5.webp" alt="SQL Server IF ELSE"> <p>We will get this output because the condition <strong>(65&gt;45)</strong> is satisfied. Therefore, the message inside the IF block is displayed:</p> <img src="//techcodeview.com/img/sql-server-tutorials/49/sql-server-if-else-6.webp" alt="SQL Server IF ELSE"> <p>Now, we will demonstrate the IF ELSE statement on the above &apos; <strong>Student&apos;</strong> table. In this example, we are going to check whether the student <strong>total marks</strong> is <strong>greater than or equal to 400</strong> or not as follows:</p> <ul> <li>When the IF condition is TRUE, we will get the student records whose total marks are greater than or equal to 550.</li> <li>If the condition is FALSE, we will get the student records whose total marks are less than 550.</li> </ul> <p>Here is the program:</p> <pre> DECLARE @Marks INT; SET @Marks = 600 ; IF @Marks &gt;= 550 BEGIN SELECT id, name, gender, age, total_marks FROM Student WHERE total_marks &gt;= 550 ORDER BY age ASC END ELSE BEGIN SELECT id, name, gender, age, total_marks FROM Student WHERE total_marks <550 order by age asc end < pre> <p>In this code, we have specified the <strong>@Marks</strong> variable to <strong>600</strong> , and the condition (600 &gt;= 550) is satisfied. Therefore, we will get the output where student records whose total marks are greater than or equal to 550 are displayed.</p> <img src="//techcodeview.com/img/sql-server-tutorials/49/sql-server-if-else-7.webp" alt="SQL Server IF ELSE"> <p>If we changed the <strong>@Marks</strong> variable to <strong>500</strong> and the condition (500 &gt;= 550) becomes false. Therefore, we will get the output where student records whose total marks are less than 550 are displayed.</p> <img src="//techcodeview.com/img/sql-server-tutorials/49/sql-server-if-else-8.webp" alt="SQL Server IF ELSE"> <h2>Nested IF ELSE Statement</h2> <p>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:</p> <pre> DECLARE @age INT; SET @age = 6; IF @age <18 50 print 'you are underage'; else begin if @age < below 50'; senior cetizen'; end; pre> <p>In this example, we are going to check whether the <strong>age is underage, below 50, or senior citizen</strong> as follows:</p> <ul> <li>If the value of the <strong>@age</strong> variable is below <strong>18</strong> , it will print the person is <strong>underage</strong> .</li> <li>If the condition is FALSE, the ELSE part will be executed that has a nested IF&#x2026;ELSE.</li> <li>If the value of the <strong>@age</strong> variable is under <strong>50</strong> , it will print <strong>below 50</strong> . Finally, if no condition is satisfied, it will print <strong>senior citizens</strong> .</li> </ul> <p>Here is the result:</p> <img src="//techcodeview.com/img/sql-server-tutorials/49/sql-server-if-else-9.webp" alt="SQL Server IF ELSE"> <p>This article gives a complete overview of how to use the SQL Server IF ELSE statement. Here we have learned:</p> <ul> <li>Variables are objects that serve as placeholders.</li> <li>The keyword BEGIN will be used to start a statement block, and the END keyword must be used to close it.</li> <li>The use of ELSE in an IF... ELSE statement is optional.</li> <li>It&apos;s also possible to nest an IF...ELSE statement inside another IF...ELSE statement. However, nesting an IF statement within another statement is bad practice because it makes the code difficult to read and maintain.</li> </ul> <hr></18></pre></550></pre></45>