if sql语句_SQL IF语句介绍和概述

if sql语句_SQL IF语句介绍和概述ifsql语句ThisarticleexplorestheusefulfunctionSQLIFstatementinSQLServer.本文探讨了SQLServer中有用的函数SQLIF语句。介绍(Introduction)Inreallife,wemakedecisionsbasedontheconditions….

大家好,又见面了,我是你们的朋友全栈君。

if sql语句

This article explores the useful function SQL IF statement in SQL Server.

本文探讨了SQL Server中有用的函数SQL IF语句。

介绍 (Introduction)

In real life, we make decisions based on the conditions. For example, look at the following conditions.

在现实生活中,我们根据条件做出决定。 例如,查看以下情况。

  • If I get a performance bonus this year, I will go for international vacation or else I’ll take a domestic vacation

    如果我今年获得绩效奖金,我将去国际度假,否则我将去国内度假

  • If the weather becomes good, I will plan to go on a bike trip or else I won’t

    如果天气转好,我会计划骑自行车旅行,否则我不会

In these examples, we decide as per the conditions. For example, if I get a bonus then only I will go for an international vacation else I will go for domestic vacations. We need to incorporate these conditions-based decisions in programming logic as well. SQL Server provides the capability to execute real-time programming logic using SQL IF Statement.

在这些示例中,我们根据条件进行决定。 例如,如果我获得奖金,那么只有我会去国际度假,否则我会去国内度假。 我们还需要将这些基于条件的决策也纳入编程逻辑。 SQL Server提供了使用SQL IF语句执行实时编程逻辑的功能。

句法 (Syntax)

In the following SQL IF Statement, it evaluates the expression, and if the condition is true, then it executes the statement mentioned in IF block otherwise statements within ELSE clause is executed.

在下面SQL IF语句中,它计算表达式,如果条件为true,则执行IF块中提到的语句,否则将执行ELSE子句中的语句。

IF (Expression )
BEGIN
  -- If the condition is TRUE then execute the following statement
  True Statements;
END
 
ELSE
BEGIN
   -- If the condition is False then execute the following statement
False Statements
END

We can understand SQL IF Statement using the following flow chart.

我们可以使用以下流程图了解SQL IF语句。

Flow Chart of SQL IF Statements
  • The condition in SQL IF Statement should return a Boolean value to evaluate

    SQL IF语句中的条件应返回一个布尔值以求值

  • We can specify a Select statement as well in a Boolean expression, but it should enclose in parentheses

    我们也可以在布尔表达式中指定Select语句,但应将其括在括号中

  • We can use BEGIN and END in the IF Statement to identify a statement block

    我们可以在IF语句中使用BEGIN和END来标识一个语句块

  • The ELSE condition is optional to use

    ELSE条件是可选使用

Let’s explore SQL IF Statement using examples.

让我们使用示例探索SQL IF语句。

示例1:布尔表达式中带有数字值的IF语句 (Example 1: IF Statement with a numeric value in a Boolean expression)

In the following example, we specified a numeric value in the Boolean expression that is always TRUE. It prints the statement for If statement because the condition is true.

在下面的示例中,我们在布尔表达式中指定了一个始终为TRUE的数值。 因为条件为真,所以它为If语句打印语句。

IF(1 = 1)
    PRINT 'Executed the statement as condition is TRUE';
    ELSE
    PRINT 'Executed the statement as condition is FALSE';

SQL IF Statement with a numeric value in a Boolean expression

If we change the condition in the Boolean expression to return FALSE, it prints statement inside ELSE.

如果我们更改布尔表达式中的条件以返回FALSE,它将在ELSE内打印语句。

IF(2<=0)
    PRINT 'Executed the statement as condition is TRUE';
    ELSE
    PRINT 'Executed the statement as condition is FALSE';

SQL IF Statement with a numeric value in a Boolean expression with FALSE condition

示例2:布尔表达式中带有变量的IF语句 (Example 2: IF Statement with a variable in a Boolean expression)

In the following example, we use a variable in the Boolean expression to execute the statement based on the condition. For example, if a student obtained more than 80% marks, he passes an examination else, he is failed.

在下面的示例中,我们在布尔表达式中使用变量来根据条件执行语句。 例如,如果学生获得超过80%的分数,则他通过了其他考试,则他不及格。

DECLARE @StudentMarks INT= 91;
IF @StudentMarks >= 80
    PRINT 'Passed, Congratulations!!';
    ELSE
    PRINT 'Failed, Try again ';

variable in a Boolean expression

示例3:布尔表达式中带有变量的多重IF语句 (Example 3: Multiple IF Statement with a variable in a Boolean expression)

We can specify multiple SQL IF Statements and execute the statement accordingly. Look at the following example

我们可以指定多个SQL IF语句并相应地执行该语句。 看下面的例子

  • If a student gets more than 90% marks, it should display a message from the first IF statement

    如果学生获得90%以上的分数,则应显示第一条IF语句中的消息

  • If a student gets more than 80% marks, it should display a message from the second IF statement

    如果学生获得超过80%的分数,则应显示第二条IF语句中的消息

  • Otherwise, it should print the message mentioned in ELSE statement

    否则,它应该打印ELSE语句中提到的消息

DECLARE @StudentMarks INT= 91;IF @StudentMarks >= 90    PRINT 'Congratulations, You are in Merit list!!';IF @StudentMarks >= 80 PRINT 'Congratulations, You are in First division list!!';  ELSE    PRINT 'Failed, Try again ';

In this example, student marks 91% satisfy the conditions for both SQL IF statements, and it prints a message for both SQL IF statements.

在此示例中,学生分数91%满足两个SQL IF语句的条件,并且为两个SQL IF语句打印一条消息。

a variable in a Boolean expression and multiple IF statements

We do not want the condition to satisfy both SQL IF statements. We should define the condition appropriately.

我们不希望条件同时满足两个SQL IF语句。 我们应该适当地定义条件。

 DECLARE @StudentMarks INT= 91;
IF @StudentMarks >= 90
    PRINT 'Congratulations, You are in Merit list!!';
IF @StudentMarks >= 80 and @StudentMarks < 90
 PRINT 'Congratulations, You are in First division list!!';
  ELSE
    PRINT 'Failed, Try again ';

In the following screenshot, we can see second IF condition is TRUE if student marks are greater than or equal to 80% and less than 90%.

在下面的屏幕截图中,如果学生分数大于或等于80%且小于90%,我们可以看到第二个IF条件为TRUE。

In the output, we can see the following

在输出中,我们可以看到以下内容

  • First, IF statement condition is TRUE. It prints the message inside the IF statement block

    首先,IF语句条件为TRUE。 它在IF语句块内打印消息

  • Second, IF statement condition is FALSE, it does not print the message inside IF statement block

    其次,IF语句条件为FALSE,它不会在IF语句块内打印消息

  • It executes the ELSE statement and prints the message for it. In this case, we have two SQL IF statements. The second IF statement evaluates to false, therefore, it executes corresponding ELSE statement

    它执行ELSE语句并为其打印消息。 在这种情况下,我们有两个SQL IF语句。 第二条IF语句的计算结果为false,因此,它将执行相应的ELSE语句

a variable in a Boolean expression and multiple IF statements

We need to be careful in specifying conditions in multiple SQL IF statement. We might get an unexpected result set without proper use of SQL IF statement.

在多个SQL IF语句中指定条件时,我们需要格外小心。 如果不正确使用SQL IF语句,我们可能会得到意外的结果集。

示例4:不带ELSE语句的IF语句 (Example 4: IF Statement without ELSE statement)

We specified above that Else statement is optional to use. We can use SQL IF statement without ELSE as well.

我们在上面指定了Else语句是可选的。 我们也可以使用没有ELSESQL IF语句。

In the following, the expression evaluates to TRUE; therefore, it prints the message.

在下面,表达式的计算结果为TRUE; 因此,它将打印该消息。

DECLARE @StudentMarks INT= 95;
IF @StudentMarks >= 90
    PRINT 'Congratulations, You are in Merit list!!';

example without ELSE statement

If the expression evaluates to FALSE, it does not return any output. We should use ELSE statement so that if an evaluation is not TRUE, we can set default output.

如果表达式的计算结果为FALSE,则不返回任何输出。 我们应该使用ELSE语句,以便如果评估结果不是TRUE,则可以设置默认输出。

example without ELSE statement

示例5:执行脚本的IF语句 (Example 5: IF Statement to execute scripts)

In the above examples, we print a message if a condition is either TRUE or FALSE. We might want to execute scripts as well once a condition is satisfied.

在上述示例中,如果条件为TRUE或FALSE,我们将打印一条消息。 一旦满足条件,我们可能还希望执行脚本。

In the following example, if sales quantity is greater than 100000000, it should select records from SalesOrderDtails table.

在下面的示例中,如果销售数量大于100000000,则应从SalesOrderDtails表中选择记录。

If the sales quantity is less than 100000000, it should select records from the SalesOrderHeader table.

如果销售数量少于100000000,则应从SalesOrderHeader表中选择记录。

 DECLARE @sales INT;
SELECT @sales = SUM(OrderQty * UnitPrice)
FROM [AdventureWorks2017].[Sales].[SalesOrderDetail];
IF @sales > 100000000
    SELECT *
    FROM [AdventureWorks2017].[Sales].[SalesOrderDetail];
    ELSE
    SELECT *
    FROM [AdventureWorks2017].[Sales].[SalesOrderHeader];

示例6:具有BEGIN和END块的IF (Example 6: IF with BEGIN and END block)

We can use BEGIN and END statement block in a SQL IF statement. Once a condition is satisfied, it executes the code inside the corresponding BEGIN and End block.

我们可以在SQL IF语句中使用BEGIN和END语句块。 一旦满足条件,它将在相应的BEGIN和End块内执行代码。

 DECLARE @StudentMarks INT= 70;
IF @StudentMarks >= 90
    BEGIN
        PRINT 'Congratulations, You are in Merit list!!';
END;
    ELSE
    BEGIN
        PRINT 'Failed, Try again ';
END;

BEGIN and END block

We can specify multiple statements as well with SQL IF statement and BEGIN END blocks. In the following query, we want to print a message from two print statements once a condition is satisfied.

我们也可以使用SQL IF语句和BEGIN END块指定多个语句。 在下面的查询中,我们希望在满足条件后从两个打印语句中打印一条消息。

  • Note: We should have an END statement with corresponding BEGIN block.注意 :我们应该有一个带有相应BEGIN块的END语句。
DECLARE @StudentMarks INT= 70;
IF @StudentMarks >= 90
    BEGIN
        PRINT 'Congratulations, You are in Merit list!!';
    Print 'Second statement.'
END;
    ELSE
    BEGIN
        PRINT 'Failed,Try again ';
    Print 'Second ELSE statement'
END;

BEGIN and END block examples with IF statement

结论 (Conclusion)

In this article, we explored the SQL IF statement and its usage with examples. We can write real-time conditions-based code using SQL IF statements. If you had comments or questions, feel free to leave them in the comments below.

在本文中,我们通过示例探讨了SQL IF语句及其用法。 我们可以使用SQL IF语句编写基于条件的实时代码。 如果您有任何意见或问题,请随时将其留在下面的评论中。

翻译自: https://www.sqlshack.com/sql-if-statement-introduction-and-overview/

if sql语句

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/160602.html原文链接:https://javaforall.net

(0)
上一篇 2022年7月16日 下午11:36
下一篇 2022年7月16日 下午11:36


相关推荐

  • php递归算法经典实例_递归算法的步骤

    php递归算法经典实例_递归算法的步骤递归算法对于任何一个编程人员来说,应该都不陌生。因为递归这个概念,无论是在PHP语言还是Java等其他编程语言中,都是大多数算法的灵魂。对于PHP新手来说,递归算法的实现原理可能不容易理解。但是只要你了解掌握了这个算法原理,就可以灵活运用递归算法实现编程中的多种功能比如实现无限分类等。递归也是入门者最需要掌握的一个基础算法技巧。下面郑州网站建设公司燚轩科技就通过具体代码示例为大家介绍PHP递归算法…

    2022年8月11日
    9
  • maven 配置阿里云仓库与公司私服

    maven 配置阿里云仓库与公司私服简述 maven 公司私服与阿里云的配置 profiles profile id maven release id repositories 公司私服 repositories profile profile id maven snapshot id profile profiles

    2026年3月18日
    2
  • 如何将XPS转成PDF?XPS转PDF的免费方法「建议收藏」

    如何将XPS转成PDF?XPS转PDF的免费方法「建议收藏」你还不知道XPS是什么?不懂这种文档要怎么打开?其实这些都不重要,只要你知道PDF就可以,教你几种将XPS转成PDF的方法,还有免费使用哦。方法一,适用于懒人党,手机党只需要打开百度或者手机中的浏览器搜索speedpdf找到并打开这款在线免费转换工具,选择XPS转PDF即可进入转换,对的,还支持将XPS转换成Word哦。添加需要转换的XPS文件后,点击转换即可,完成后直接下载。整个过程只需要几分钟,是不是超简单?当然如果你在转换之前有登录,还可以在账户中的转换记录查看所有转换记录和下载转换完成的文

    2022年6月4日
    82
  • 利用QPixmap显示图片

    利用QPixmap显示图片利用 QPixmap 显示图片我们来实现在窗口上显示图片 并将图片进行平移 缩放 旋转和扭曲 这里我是利用 QPixmap 类来实现图片显示的 一 利用 QPixmap 显示图片 1 将以前的工程文件夹进行复制备份 我们这里将工程文件夹改名为 painter05 经常备份工程目录 是个很好的习惯 2 在工程文件夹的 debug 文件夹中新建文件夹 我这里命名为

    2026年3月19日
    2
  • 7-10 公路村村通(并查集kruskal)

    7-10 公路村村通(并查集kruskal)最小生成树题目链接现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本,求使每个村落都有公路连通所需要的最低成本。输入格式:输入数据包括城镇数目正整数N(≤1000)和候选道路数目M(≤3N);随后的M行对应M条道路,每行给出3个正整数,分别是该条道路直接连通的两个城镇的编号以及该道路改建的预算成本。为简单起见,城镇从1到N编号。输出格式:输出村村通需要的最低成本。如果输入数据不足以保证畅通,则输出−1,表示需要建设更多公路。输入样例:6 151 2 51 3 3

    2022年8月8日
    8
  • spring cloud 入门系列二:使用Eureka 进行服务治理「建议收藏」

    服务治理可以说是微服务架构中最为核心和基础的模块,它主要用来实现各个微服务实例的自动化注册和发现。SpringCloudEureka是SpringCloudNetflix微服务套件的一部分

    2022年2月16日
    42

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号