1

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

  1. 名字、姓氏和出生日期:这三个都不为空
  2. ID 和 DOB 不为空
  3. SSN 和 DOB 不为空
  4. ID 和 Group Number 不为空

下面是我的代码。当我通过提供名字、姓氏和出生日期来运行它时(满足条件 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 
4

2 回答 2

5

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

至于你的语法错误,你有多余的BEGINand 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 
于 2014-01-31T21:05:15.900 回答
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;
于 2014-01-31T21:31:40.563 回答