在 sql 中使用嵌套的 IF ELSE 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21489960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 00:57:13  来源:igfitidea点击:

Using Nested IF ELSE statements in sql

sqlnested-if

提问by Hart CO

When any one of the following conditions is met, I want the code to go to the next execution step:

当满足以下任一条件时,我希望代码进入下一个执行步骤:

  1. First Name, Last Name and DOB : all three are not blank
  2. ID and DOB are not blank
  3. SSN and DOB are not blank
  4. ID and Group Number are not blank
  1. 名字、姓氏和出生日期:这三个都不为空
  2. ID和DOB不为空
  3. SSN和DOB不为空
  4. ID和组号不为空

Below is the code I have. When I run it by providing First Name, Last Name and DOB (condition 1 is satisfied), it still fails saying condition 4 is not met. Can someone tell me what am I doing wrong?

下面是我的代码。当我通过提供名字、姓氏和出生日期(满足条件 1)来运行它时,它仍然失败,说不满足条件 4。有人能告诉我我做错了什么吗?

IF ( ( @FirstName IS NULL 
        OR Len(Ltrim(@FirstName)) = 0 ) 
     AND ( @LastName IS NULL 
            OR Len(Ltrim(@LastName)) = 0 ) 
     AND ( @DOB IS NULL ) ) 
  BEGIN 
      INSERT INTO @ValidationError 
                  (errormessage) 
      VALUES      ( 'first name, last name and Date of Birth must be specified.' 
      ) 
  END 
ELSE 
  BEGIN 
      IF ( @DOB IS NULL 
           AND @Id IS NULL ) 
        BEGIN 
            INSERT INTO @ValidationError 
                        (errormessage) 
            VALUES      ( 'Date of Birth and Id must be specified.' ) 
        END 
      ELSE 
        BEGIN 
            IF ( @DOB IS NULL 
                 AND @SSN IS NULL ) 
              BEGIN 
                  INSERT INTO @ValidationError 
                              (errormessage) 
                  VALUES      ( 'Date of Birth and SSN must be specified.' ) 
              END 
            ELSE 
              BEGIN 
                  IF ( @Id IS NULL 
                       AND @GroupNumber IS NULL ) 
                    BEGIN 
                        INSERT INTO @ValidationError 
                                    (errormessage) 
                        VALUES      ( 'Id and Group Number must be specified.' ) 
                    END 
              END 
        END 
  END 

回答by Hart CO

A CASEstatement would be simpler:

一个CASE说法会更简单:

INSERT INTO @ValidationError  (errormessage)  
SELECT CASE WHEN Criteria1 THEN 'first name, last name and Date of Birth must be specified.'
            WHEN Criteria2 THEN 'Date of Birth and Id must be specified.'
            WHEN Criteria3 THEN 'Date of Birth and SSN must be specified.'  
            WHEN Criteria4 THEN 'Id and Group Number must be specified.'        
       END

As far as the error in your syntax, you've got extraneous BEGINand END, I believe the following will work:

至于你的语法错误,你有无关紧要的BEGINEND,我相信以下方法会起作用:

IF ( ( @FirstName IS NULL OR Len(Ltrim(@FirstName)) = 0 ) 
     AND ( @LastName IS NULL OR Len(Ltrim(@LastName)) = 0 ) 
     AND ( @DOB IS NULL ) ) 
    BEGIN 
          INSERT INTO @ValidationError 
                      (errormessage) 
          VALUES      ( 'first name, last name and Date of Birth must be specified.') 
    END 
ELSE IF ( @DOB IS NULL AND @Id IS NULL ) 
    BEGIN 
        INSERT INTO @ValidationError 
                    (errormessage) 
        VALUES      ( 'Date of Birth and Id must be specified.' ) 
    END 
ELSE IF ( @DOB IS NULL AND @SSN IS NULL ) 
    BEGIN 
        INSERT INTO @ValidationError 
                    (errormessage) 
        VALUES      ( 'Date of Birth and SSN must be specified.' ) 
    END 
ELSE IF ( @Id IS NULL AND @GroupNumber IS NULL ) 
    BEGIN 
        INSERT INTO @ValidationError 
                    (errormessage) 
        VALUES      ( 'Id and Group Number must be specified.' ) 
    END 

回答by Gordon Linoff

First, your logic is failing because you have if/then/elseif. In other words, if something has a first name, last name, and date of birth, then they pass the first criteria. What do you do? You go and test the next one. Nope. They passed, so you want to continue.

首先,你的逻辑失败了,因为你有if/then/elseif. 换句话说,如果某物有名字、姓氏和出生日期,那么它们就通过了第一个标准。你做什么工作?你去测试下一个。不。他们过去了,所以你想继续。

You are testing for failing all the criteria, not for failing one of them. Your error messages reflect this. There aren't four error messages. There is just one. Basically, it is the concatenation of all of yours, because none of the conditions would be met.

您正在测试是否满足所有标准,而不是其中之一。您的错误消息反映了这一点。没有四个错误消息。只有一个。基本上,它是你所有的串联,因为没有一个条件会被满足。

The structure should be:

结构应该是:

if (criteria1 failse) {
    if (criteria2 fails) {
        if (criteria3 fails) {
            if (criteria4 fails) {
                everything failse
            }
        }
    }
}

See, if nothing passes, then you can't arbitrarily choose which one fails.

看,如果什么都没有通过,那么你就不能随意选择哪一个失败了。

You can wrap this into a single query:

您可以将其包装到单个查询中:

insert into @ValidationError(errormessage)  
    SELECT 'You need to specify one of the following: '+
           'first name, last name and Date of Birth must be specified; ' +
           'Date of Birth and Id must be specified; ' +
           'Date of Birth and SSN must be specified; ' + 
           'Id and Group Number must be specified.'        
    from (select (case when not ( ( @FirstName IS NULL OR Len(Ltrim(@FirstName)) = 0 ) AND
                              ( @LastName IS NULL OR Len(Ltrim(@LastName)) = 0 ) AND
                              ( @DOB IS NULL )
                            )
                       then 'Criteria1'
                       when not ( @DOB IS NULL AND @Id IS NULL ) 
                       then 'Criteria2'
                       when not ( @DOB IS NULL AND @SSN IS NULL )
                       then 'Criteria3'
                       when not ( @Id IS NULL AND @GroupNumber IS NULL )
                  end) as whichsuccess
         ) t
    where whichsuccess is null;