指示符变量数组

当您将预编译器选项 COMPATIBILITY_MODE 设置为 ORA 时,可以将指示符数组用于非动态 FETCH INTO、INSERT、UPDATE 和 DELETE 语句。

指示符变量数组是与特定主变量数组或结构数组关联的 short 数据类型变量。 指示符变量数组中的每个指示符变量元素可包含 0 或 -1 值,此值指示关联主变量或结构是否包含空值。 如果指示符变量值小于零,那么这表明相应的数组值为 NULL。

在 FETCH INTO 语句中,可使用指示符变量数组来确定数组变量的任何元素是否为 null。

可使用关键字 INDICATOR 来标识指示符变量,如以下示例所示。

在以下示例中,声明了称为 bonus_ind 的指示符变量数组。 bonus_ind 指示符变量数组最多可具有 100 个元素,基数与 bonus 数组变量的基数相同。 访存数据时,如果 bonus 的值为 NULL,那么 bonus_ind 中的值为负数。EXEC SQL BEGIN DECLARE SECTION;

char empno[100][8];

char lastname[100][15];

short edlevel[100];

double bonus[100];

short bonus_ind[100];

EXEC SQL END DECLARE SECTION;

EXEC SQL DECLARE empcr CURSOR FOR

SELECT empno, lastname, edlevel, bonus

FROM employee

WHERE workdept = 'D21';

EXEC SQL OPEN empcr;

EXEC SQL WHENEVER NOT FOUND GOTO end_fetch;

while (1) {

EXEC SQL FETCH empcr INTO :empno :lastname :edlevel,

:bonus INDICATOR :bonus_ind

...

...

}

end_fetch:

EXEC SQL CLOSE empcr;

指示符变量不是由 INDICATOR 关键字标识,而是可以直接跟在其相应的主变量之后,如以下示例中所示:EXEC SQL FETCH empcr INTO :empno :lastname :edlevel, :bonus:bonus_ind

在以下示例中,声明了指示符变量数组 ind_in1 和 ind_in2。 它最多可有三个元素,与数组变量

arr_in1 和 arr_in2 的基数相同。 如果 ind_in1 或

ind_in2 的值为负,那么将针对相应的 arr_in1 或 arr_in2 值插入 NULL 值。// Declare host & indicator variablesof array size 3

EXEC SQL BEGIN DECLARE SECTION;

sqlint32 arr_in1[3];

char arr_in2[3][11];

short ind_in1[3]; // indicator array size is same as host

// variable’s array size

short ind_in2[3]; // note here indicator array size is greater

// than host variable’s array size

EXEC SQL END DECLARE SECTION;

...

// Populating the arrays.

for ( i = 0; i < 3; i++)

{

arr_in1[i] = i + 1;

sprintf(arr_in2[i], "hello%d", arr_in1[i]);

}

ind_in1[0] = 0;

ind_in1[1] = SQL_NULL_DATA; // Mark it as a NULL data

ind_in1[2] = 0;

ind_in2[0] = 0;

ind_in2[1] = 0;

ind_in2[2] = SQL_NULL_DATA; // Mark it as a NULL data

// ‘arr_in1’ & ‘arr_in2’ are host variable arrays

// ‘ind_in1’ & ‘ind_in2’ are indicator variable arrays

EXEC SQL INSERT into tbl1 values (:arr_in1 :ind_in1, :arr_in2 :ind_in2);

// The tb11 table now contains the following rows:

C1 C2

----------- -----------

1 hello1

hello2 // c1 is set to NULL as indicator is set

3 // c2 is set to NULL as indicator is set如果指示符变量数组的基数与相应的主变量数组的基数不匹配,那么将返回错误。

在以下示例中,声明了 MyStructInd 指示符结构数组。// declaring indicator structure array of size 3

EXEC SQL BEGIN DECLARE SECTION;

...

struct MyStructInd

{

short c1_ind;

short c2_ind;

} MyStructVarInd[3];

EXEC SQL END DECLARE SECTION;

...

// using structure array host variables & indicators structure type

// array while executing FETCH statement

// ‘MyStructVar’ is structure array for host variables

// ‘MyStructVarInd’ is structure array for indicators

EXEC SQL FETCH cur INTO :MyStructVar :MyStructVarInd;重要信息: 使用指示符结构数组时,必须满足以下条件。

指示符结构数组的基数必须等于或大于结构数组的基数。

指示符结构数组中的所有成员都必须使用 short 数据类型。

指示符结构数组中的成员数必须与相应结构数组中的成员数匹配。

对于 INSERT、UPDATE 和 DELETE 操作,应用程序必须确保所有指示符变量都使用 0 或

SQL_NULL_DATA (-1) 进行初始化。

成功处理的总行数存储在 sqlca.sqlerrd[3] 字段中。 但是,sqlca.sqlerrd[3] 字段不代表在 INSERT、UPDATE 或

DELETE 操作过程中成功落实的行数。 受 INSERT、UPDATE 或 DELETE 操作影响的总行数存储在 sqlca.sqlerrd[2] 字段中。