logo

خادم SQL إذا كان آخر

تعد عبارة IF جزءًا من وظيفة التحكم في التدفق في SQL Server. عادةً ما يكون بيان اتخاذ القرار بلغات برمجة مختلفة تقوم بإرجاع قيمة بناءً على الشروط المحددة . ينفذ هذا البيان الكود المكتوب في كتلة IF عندما يتم تقييم الشرط المحدد على أنه صحيح وعندما يتم تقييم الشرط على خطأ، فسيتم تنفيذ عبارة ELSE.

بيان IF

فيما يلي بناء الجملة الذي يوضح استخدام هذا البيان في SQL Server:

 IF boolean_expression BEGIN { statement_block } END 

في بناء الجملة أعلاه، statement_block في ال البداية...النهاية يتم تنفيذ الكتلة عندما تعبير منطقي راض عن الشرط. وإلا، فسيتم تخطي هذه الكتلة، ويتم نقل عنصر التحكم في البرنامج إلى العبارة بعد نهاية الكلمة الرئيسية. يجب أن نعرف أنه إذا كان التعبير يحتوي على أ يختار بيان، نحن بحاجة إلى أرفقها بين قوسين .

مثال

دعونا نرى المثال لفهم عبارة IF بدون كتلة ELSE. سيعرض المثال أدناه النتيجة عند استيفاء الشرط. وبخلاف ذلك، يتم نقل التحكم في البرنامج إلى العبارة بعد الكلمة الأساسية END، إن وجدت.

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

تنفيذ البيان سوف يعطي الإخراج أدناه:

خادم SQL إذا كان آخر

والآن سوف نوضح ذلك فيما يلي طالب' جدول يحتوي على البيانات التالية:

خادم SQL إذا كان آخر

وفيما يلي مثال آخر يحصل على مجموع علامات لطالب مختار من ' طالب' الجدول في قاعدة البيانات النموذجية ثم يطبع a رسالة اذا كانت أكبر من 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 إذا كان آخر

إذا أردنا رؤية رسالة الإخراج أعلاه، فيجب علينا النقر فوق رسائل فاتورة غير مدفوعة:

خادم SQL إذا كان آخر

بيان IF-ELSE

في السيناريو الواقعي، نحتاج إلى تنفيذ بعض الإجراءات عندما يكون الشرط في عبارة IF صحيحًا أو خطأ. في هذه الحالة، تكون عبارة IF…ELSE مفيدة. ينفذ هذا البيان كتلة عبارة ELSE عندما يتم تقييم الشرط الموجود في عبارة IF على أنه خطأ.

فيما يلي بناء الجملة الذي يوضح استخدام عبارة IF ELSE في SQL Server :

 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 

مثال

دعونا نرى المثال لفهم عبارة IF مع كتلة ELSE. سيعرض المثال أدناه الرسالة ' تهانينا! يمكنك اجتياز الامتحان ' عند استيفاء شرط 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>