Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在sql中使用嵌套的IF-ELSE语句_Sql_Nested If - Fatal编程技术网

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

在sql中使用嵌套的IF-ELSE语句,sql,nested-if,Sql,Nested If,当满足以下任一条件时,我希望代码转到下一个执行步骤: 名字、姓氏和出生日期:这三个都不是空的 ID和DOB不是空的 SSN和DOB不是空的 ID和组号不为空 下面是我的代码。当我通过提供名字、姓氏和DOB(满足条件1)来运行它时,它仍然无法表示不满足条件4。有人能告诉我我做错了什么吗 IF ( ( @FirstName IS NULL OR Len(Ltrim(@FirstName)) = 0 ) AND ( @LastName IS NULL

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

  • 名字、姓氏和出生日期:这三个都不是空的
  • ID和DOB不是空的
  • SSN和DOB不是空的
  • ID和组号不为空
  • 下面是我的代码。当我通过提供名字、姓氏和DOB(满足条件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 
    

    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
    
    至于语法中的错误,您已经有了无关的
    开始
    结束
    ,我相信以下方法可以奏效:

    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 
    

    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
    
    至于语法中的错误,您已经有了无关的
    开始
    结束
    ,我相信以下方法可以奏效:

    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 
    

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

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

    结构应为:

    if (criteria1 failse) {
        if (criteria2 fails) {
            if (criteria3 fails) {
                if (criteria4 fails) {
                    everything failse
                }
            }
        }
    }
    
    看,如果没有通过,那么你不能随意选择哪一个失败

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

    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;
    

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

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

    结构应为:

    if (criteria1 failse) {
        if (criteria2 fails) {
            if (criteria3 fails) {
                if (criteria4 fails) {
                    everything failse
                }
            }
        }
    }
    
    看,如果没有通过,那么你不能随意选择哪一个失败

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

    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;
    

    我试着按照下面的两个答案去做,但都做不出来。相反,每次插入@ValidationError表时,我都会更新一个参数,如果参数没有更新,则使用GOTO标签(这意味着表中没有插入任何内容)。GOTO是我的一个老习惯,我刚刚记得,从FORTRAN编程的日子起。我试着遵循下面的两个答案,但没办法解决。相反,每次插入@ValidationError表时,我都会更新一个参数,如果参数没有更新(这意味着表中没有插入任何内容),则使用GOTO标签。GOTO是我的一个老习惯,我刚刚记得,从FORTRAN编程的日子起。