SQLSTATE 会是 00000 并带有负空指示符吗?

Will SQLSTATE ever be 00000 with a negative null indicator?

是否可以使用 SQLSTATE 为 00000 的负空指示符?

我想知道某些操作(如 if 语句)是否需要同时检查 SQLSTATE 和 null 指示符值。这是我正在使用的示例:

EXEC SQL                                                                           
  SELECT 1 INTO :x1RcdFound:x1IndicatorVariable                                               
    FROM table1                                                                   
      WHERE value = :givenValue                                                    
       AND value2 = 'helloWorld';              

//this is where I am wondering if both checks need to happen                   
**if ( sqlstate = SQL_OK and x1RcdFound = 1 );**

虽然这是一个简单的例子,但有时会运行通过一个大循环,或者需要使用多个语句,节省一点运行时间有助于长运行。这是编写尽可能简洁代码的努力的一部分,这在过程语言中经常被忽视。

在给定的示例中,由于您选择的是文字值,因此结果可能为 NULL 的唯一情况是未找到数据,在这种情况下,SQLSTATE 值将为 02000。换句话说,如果 SQLSTATE 是 00000 那么结果不能为 NULL。

如果 NULL 值是可能的,那么你需要一个 NULL 指示器,那么是的,你必须在获得 00000 的 SQLSTATE 后检查它。

但是,仅当列可能为 NULL 时才需要 NULL 指示符。

在特定情况下,您提供的代码不会发生这种情况,您不需要 NULL 指示符。

EXEC SQL                                                                           
  SELECT 1 INTO :x1RcdFound
    FROM table1                                                                   
      WHERE value = :givenValue                                                    
       AND value2 = 'helloWorld';

您要么得到 1,要么没有行。这与包含 NULL 列的 RS 不同。

事实上,如果没有行,您将得到 SQLSTATE = '02000' --> NO_DATA

我看到的大多数像你这样的代码都利用了这样一个事实,即如果没有行,或者如果出现问题,主机变量是不变的......

dcl-s xlRcdFound ind;

// note the definition of xlRcdFound as indicator
//   ie.  char(1) used as a Boolean.
xlRcdFound = *OFF;
EXEC SQL                                                                           
  SELECT '1' INTO :x1RcdFound
    FROM table1                                                                   
      WHERE value = :givenValue                                                    
       AND value2 = 'helloWorld';

if SqlExceptionOccurred(SQLSTATE:*ON);
  //report SQL exception
elseif xlRcdFound;
  //do something
endif;


dcl-proc SqlExceptionOccurred;
dcl-pi *n ind;
   mySqlState char(5) value;
   allowNoData ind value;
end-pi;
  if allowNoData;
     return %subst(mySqlState:1:2) > '02';
  endif;
     return %subst(mySqlState:1:2) >= '02';
end-proc;