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年5月20日
    35
  • linux下go语言代理

    linux下go语言代理exportGO111MODULE=onexportGOPROXY=https://goproxy.cn

    2022年7月25日
    15
  • awk从放弃到入门(9):awk数组详解

    awk从放弃到入门(9):awk数组详解 这篇文章中的知识点是建立在前文的基础上的,如果你还没有掌握前文中的知识,请先参考之前的文章。注:在阅读这篇文章之前,最好已经了解了一些开发的基本语法,比如,for循环、数组的基本使用等,否则在阅读时有可能遇到障碍。 前文中提及过,awk其实可以算作一门脚本语言,因为它包含了一个脚本语言的各种语法结构,比如条件判断语句,比如循环语句,那么,awk中能否使用"数组"呢?必须能啊,今天我们就来聊聊…

    2022年7月19日
    16
  • 树莓派命令连接wifi_树莓派连接无线网wifi配置方法

    树莓派命令连接wifi_树莓派连接无线网wifi配置方法Wifi配置我的Wifi配置基本上是跟着这个教程来的,下面将过程简述,并解释每个命令/语句的作用。1、检查USB无线网卡是否已经正确识别将无线USB网卡插入树莓派后启动树莓派,比较不建议热插拔,因为插入的一瞬间会有比较高的电流,如果电源输出不够可能导致树莓派重启。用自己的方法进入shell界面后输入命令:lsusb如果树莓派已经正常识别,在显示类似于如下的信息中可以看到你的USB无线网卡设备ID和…

    2022年6月6日
    160
  • wps怎么做时间线_wps中的word文档如何制作时间轴「建议收藏」

    wps中的word文档如何制作时间轴很多小伙伴不知道wps中的word文档还可以制作时间轴哦,下面小编介绍一下具体办法。具体如下:1.打开一个空白的word文档,点击菜单栏的【插入】,单击选择一个带箭头的线条,在文档上面留一点空白写标题,然后插入线条拉到底部,还可以设置线条颜色,虚实等样式。2.点击工具栏中的【文本框】旁边的倒三角,再点击【多行文字】,插入一个大的文本框3.可以根据需要进行文…

    2022年4月10日
    197
  • 伪静态规则写法RewriteRule-htaccess详细语法使用

    伪静态规则写法RewriteRule-htaccess详细语法使用伪静态实际上是利用PHP把当前地址解析成另一种方法来访问网站,要学伪静态规则的写法,要懂一点正则一、正则表达式教程有一个经典的教程:正则表达式30分钟入门教程常用正则如下:.换行符以外的所有字符\w

    2022年7月4日
    22

发表回复

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

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