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

时间:2014-01-31 21:01:00

标签: sql nested-if

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

  1. 名字,姓氏和DOB:这三个都不是空白
  2. ID和DOB不为空白
  3. SSN和DOB不是空白
  4. ID和组号不为空
  5. 以下是我的代码。当我通过提供名字,姓氏和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 
    

2 个答案:

答案 0 :(得分:3)

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

就语法错误而言,你有无关的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 

答案 1 :(得分:0)

首先,您的逻辑失败,因为您有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;