【问题标题】:t sql "select case" vs "if ... else" and explaination about "begin"t sql "select case" vs "if ... else" 以及关于 "begin" 的解释
【发布时间】:2013-12-18 21:51:30
【问题描述】:

我对t sql的经验很少,我必须写一个存储。

这是我存储的:

USE myDatabase
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[myStored]
(
@myPar1 INT,
@myPar2 SMALLDATETIME
)

AS

BEGIN
  SET NOCOUNT ON

  IF EXISTS (
            SELECT 
            1 

            FROM 
            myTable1 

            WHERE 
            myPar1 = @myPar1
            AND myPar2 = @myPar2
            )

    BEGIN
      DELETE FROM  
      myTable1  

      WHERE 
      myPar1 = @myPar1
      AND myPar2 = @myPar2
    END

  ELSE 

    IF EXISTS (
              SELECT 
              1 

              FROM 
              myTable2 

              WHERE 
              myPar2 = @myPar2
              )

      BEGIN
        INSERT INTO  
        myTable1
        (myField1, myField2, myField3, myField4)

        VALUES
        (@myPar1, @myPar2, '', 1)
      END    

    ELSE

      IF EXISTS (
                SELECT 
                1 

                FROM 
                myTable3 

                WHERE 
                myPar2 = @myPar2
                )

        BEGIN
          INSERT INTO  
          myTable1
          (myField1, myField2, myField3, myField4)

          VALUES
          (@myPar1, @myPar2, '', 1)
        END
END

这些是我的问题:

1 - 是否存在宏观误差?

2 - 有人建议使用“SELECT CASE”别人使用“IF ... ELSE”,有什么区别?什么是我存储的最佳选择?

3 - 我不确定“BEGIN ... END”语句的使用,尤其是与“IF ... ELSE”语句的结合。这是什么意思?是否有必要将“BEGIN ... END”放在“IF ... ELSE”语句中?也用于执行单条指令?

【问题讨论】:

    标签: sql sql-server tsql stored-procedures switch-statement


    【解决方案1】:

    对于单个 IF 语句

    IF (Some Condition)     --<-- If condition is true control will get inside the 
     BEGIN                     -- BEGIN ..END Block and execute the Code inisde
       /* Your Code Here*/
     END
    

    所有单个 IF 语句都将独立检查条件。

    ONE IF 与 ONE ELSE

    IF (Some Condition)     --<-- If condition is true control will get inside the 
     BEGIN                     -- BEGIN ..END Block and execute the Code inisde
       /* Your Code Here*/     -- IF not true control will jump to Else block
     END
    ELSE       --<-- You dont mention any condition here
     BEGIN                    
       /* Your Code Here*/
     END
    

    如果为真,则只有一个代码块将执行,然后执行第一个代码块 Otherwsie ELSE 代码块。

    多个 IF 和 ELSE

    IF (Some Condition)     --<--1) If condition is true control will get inside the 
      BEGIN                     -- BEGIN ..END Block and execute the Code inisde
        /* Your Code Here*/     -- IF not true control will check next ELSE IF Blocl
      END
    ELSE IF (Some Condition)       --<--2) This Condition will be checked
      BEGIN                    
          /* Your Code Here*/
      END
    ELSE IF (Some Condition)       --<--3) This Condition will be checked
      BEGIN                    
         /* Your Code Here*/
      END
    ELSE                --<-- No condition is given here Executes if non of
      BEGIN                --the previous IFs were true just like a Default value   
         /* Your Code Here*/
      END
    

    只有第一个代码块将被执行,如果条件为真,其余的将被忽略。

    BEGIN ..END 块

    在任何 IF、ELSE IF 或 ELSE 之后,如果您正在执行多个语句,则必须将它们包装在 BEGIN..END 块中。如果您只执行一个语句,则没有必要,但始终使用 BEGIN END 块是一种好习惯,这样可以更容易地阅读您的代码。

    您的程序

    我已经取出 ELSE 语句,让每个 IF 语句独立检查给定的条件,现在您对如何处理 IF 和 ELSE 有了一些想法,所以自己尝试一下,因为我不知道您在这里尝试应用什么逻辑.

    CREATE PROCEDURE [dbo].[myStored]
    (
    @myPar1 INT,
    @myPar2 SMALLDATETIME
    )
    
    AS
    
    BEGIN
      SET NOCOUNT ON
    
      IF EXISTS (SELECT 1 FROM myTable1 WHERE myPar1 = @myPar1
                AND myPar2 = @myPar2)
        BEGIN
          DELETE FROM  myTable1  
          WHERE myPar1 = @myPar1 AND myPar2 = @myPar2
        END
    
    
        IF EXISTS (SELECT 1 FROM myTable2 WHERE myPar2 = @myPar2)
          BEGIN
             INSERT INTO  myTable1(myField1, myField2, myField3, myField4)
             VALUES(@myPar1, @myPar2, '', 1)
          END    
    
    
          IF EXISTS (SELECT 1 FROM myTable3  WHERE myPar2 = @myPar2)
            BEGIN
               INSERT INTO  myTable1(myField1, myField2, myField3, myField4)
               VALUES(@myPar1, @myPar2, '', 1)
            END
    END
    

    【讨论】:

    • 没问题,很高兴它有帮助:)
    • 也许我无法集中注意力,但看起来这两个 INSERT 是相同的。使用IF EXISTS ( ... ) OR EXISTS ( ... ) INSERT ... 会简化事情。
    【解决方案2】:
    1. 我没有看到任何宏观错误
    2. IF ELSE 语句是在您的情况下用作插入或删除数据的语句,具体取决于您的 IF 子句的结果。 SELECT CASE 表达式可用于根据 SELECT 语句中的数据获取结果表达式,但不适用于根据数据结果应用算法。
    3. 查看 BEGIN END 语句,如代码 { code } 中的大括号。在 T-SQL 中放置 BEGIN END 语句不是强制性的。在我看来,最好使用它,因为它清楚地显示了您的算法的开始和结束位置。此外,如果将来有人必须处理您的代码,使用 BEGIN END 会更容易理解,并且他会更容易看到您的代码背后的逻辑。

    【讨论】:

      【解决方案3】:
      1. 此脚本中没有错误。

      2. Case 语句用于表达式评估,而不用于语句执行。因此,不能在当前要求中使用。有关案例陈述的更多详细信息,请查看http://msdn.microsoft.com/en-us/library/ms181765.aspx

      3. 通常,语句可以是单个语句或复合语句。复合语句是语句的组合。对于 IF 条件,可以指定单个或复合语句,并且 SQL 服务器选择将其分组到 BEGIN .. END 中(与少数其他数据库/编程语言不同)。 ELSE 也是如此。因此,IF 后面应该跟 BEGIN...END,ELSE 后面应该跟 BEGIN...END。更多详情请参考http://msdn.microsoft.com/en-us/library/ms182717(v=sql.120).aspx

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-09
        • 2016-02-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-10
        相关资源
        最近更新 更多