更新集合的示例

假设把表 table1 某一行的 set_col 列定义为 SET ,并且有一行的值为 {1,8,4,5,2}。下面的 GBase 8s ESQL/C 程序把值为 4 的元素改为 10
main
          {
          EXEC SQL BEGIN DECLARE SECTION;
          int a;
          collection b;
          EXEC SQL END DECLARE SECTION;
          
          EXEC SQL allocate collection :b;
          EXEC SQL select set_col into :b from table1
          where int_col = 6;
          
          EXEC SQL declare set_curs cursor for
          select * from table(:b) for update;
          EXEC SQL open set_curs;
          while (SQLCODE != SQLNOTFOUND)
          {
          EXEC SQL fetch set_curs into :a;
          if (a = 4)
          {
          EXEC SQL update table(:b)(x)
          set x = 10 where current of set_curs;
          break;
          }
          }
          EXEC SQL update table1 set set_col = :b
          where int_col = 6;
          EXEC SQL deallocate collection :b;
          EXEC SQL close set_curs;
          EXEC SQL free set_curs;
          }

执行这个 GBase 8s ESQL/C 程序以后,表 table1set_col 列具有值 {1,8,10,5,2}。

这个 GBase 8s ESQL/C 程序定义了两个 collection 变量,ab,并且从 table1 中选择了一个 SET 到 b.。WHERE 子句确保只返回一行。然后程序定义一个集合游标,它从 b 中一次选择一个元素到 a。当程序找到值为 4 的元素时,第一个 UPDATE 语句把该元素的值改为 10 并退出循环。

在第一个 UPDATE 语句中,x 是一个派生列名,用来在集合派生表中更新当前元素。第二个 UPDATE 语句用新的集合更新基表 table1

有关如何在 GBase 8s ESQL/C 中使用 collection 主变量的信息,请参阅 GBase 8s ESQL/C 程序员手册 中关于复杂数据类型的讨论。