云数据库 GAUSSDB-赋值语句:示例

时间:2024-11-02 18:46:47

示例

gaussdb=# DROP TABLE IF EXISTS customers;
NOTICE:  table "customers" does not exist, skipping
DROP TABLE
gaussdb=# CREATE TABLE customers(id int,name varchar);
CREATE TABLE
gaussdb=# INSERT INTO customers VALUES(1,'ab');
gaussdb=# DECLARE
    my_id integer;
BEGIN
    select id into my_id from customers limit 1; -- 赋值
END;
/
ANONYMOUS BLOCK EXECUTE
gaussdb=# DECLARE
    type id_list is varray(6) of customers.id%type;
    id_arr id_list;
BEGIN
    select id bulk collect into id_arr from customers order by id DESC limit 20; -- 批量赋值
END;
/
ANONYMOUS BLOCK EXECUTE
  • BULK COLLECT INTO 只支持批量赋值给数组或集合。数组类型合理使用LIMIT字段避免操作过量数据导致性能下降。
  • 对于数组变量,小括号"()"将优先识别为下标,因此对于带括号的表达式,不支持写在数组变量后面。如对于select (1+3) into va(5),不支持写为select into va(5) (1+3)或select into va[5] (1+3)。
  • BULK COLLECT INTO 只支持在ORA兼容性数据库下使用。
support.huaweicloud.com/distributed-devg-v3-gaussdb/gaussdb-12-0718.html