什么是高级?这就叫高级—openGauss(162)

52 阅读1分钟

#openGauss #入门 #安装 #数据库 #开源

知识来源:docs-opengauss.osinfra.cn/zh/

示例:

openGauss=# CREATE TABLE customers(id integer);
openGauss=# INSERT INTO customers VALUES(generate_series(1,100)); 
openGauss=# DECLARE
    my_id integer;
BEGIN
    select id into my_id from customers limit 1; -- 赋值
END;
/

openGauss=# 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;
/

openGauss=# CREATE TABLE test(a integer);
openGauss=# CREATE OR REPLACE FUNCTION check_test() RETURNS integer
    language plpgsql
    AS $function$
    DECLARE
	    b integer;
    BEGIN
	    SELECT INTO b a FROM test WHERE a=1; -- 返回空结果集
            RETURN b;
    END;
    $function$;
openGauss=# SELECT check_test();
ERROR:  query returned no rows when process INTO

须知:

  • BULK COLLECT INTO 只支持批量赋值给数组或集合。集合类型合理使用LIMIT字段避免操作过量数据导致性能下降。
  • INTO/BULK COLLECT INTO 只支持4层以下Record类型直接嵌套。
  • 返回空结果集需要数据库初始化使用PG兼容参数,配置GUC参数set behavior_compat_options = 'select_into_return_null'为开启配置GUC参数set behavior_compat_options = ''则关闭。
  • 对于数组变量,小括号"()“将优先识别为下标,因此对于带括号的表达式,不支持写在数组变量后面。如对于select (1+3) into va(5),不支持写为select into va(5) (1+3)或select into va[5] (1+3)。
  • INSERT INTO、UPDATE INTO、DELETE INTO、EXECUTION INTO不支持返回空结果集。
  • 给多个变量赋值时,由于后面的变量存在语法错误,所以均不赋值。
  • 不支持分布式。

#openGauss #入门 #安装 #数据库 #开源

知识来源:docs-opengauss.osinfra.cn/zh/