云数据库 GaussDB-record:示例

时间:2023-11-01 16:18:34

示例

  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
下面示例中用到的表定义如下:gaussdb=# \d emp_rec                Table "public.emp_rec"  Column  |              Type              | Modifiers ----------+--------------------------------+----------- empno    | numeric(4,0)                   | not null ename    | character varying(10)          |  job      | character varying(9)           |  mgr      | numeric(4,0)                   |  hiredate | timestamp(0) without time zone |  sal      | numeric(7,2)                   |  comm     | numeric(7,2)                   |  deptno   | numeric(2,0)                   | --演示在函数中对数组进行操作。gaussdb=# CREATE OR REPLACE FUNCTION regress_record(p_w VARCHAR2)RETURNSVARCHAR2  AS $$DECLARE   --声明一个record类型.   type rec_type is record (name  varchar2(100), epno int);   employer rec_type;   --使用%type声明record类型   type rec_type1 is record (name  emp_rec.ename%type, epno int not null :=10);   employer1 rec_type1;   --声明带有默认值的record类型   type rec_type2 is record (         name varchar2 not null := 'SCOTT',          epno int not null :=10);    employer2 rec_type2;    CURSOR C1 IS  select ename,empno from emp_rec order by 1 limit 1;            BEGIN      --对一个record类型的变量的成员赋值。     employer.name := 'WARD';     employer.epno = 18;     raise info 'employer name: % , epno:%', employer.name, employer.epno;     --将一个record类型的变量赋值给另一个变量。     employer1 := employer;     raise info 'employer1 name: % , epno: %',employer1.name, employer1.epno;               --将一个record类型变量赋值为NULL。     employer1 := NULL;     raise info 'employer1 name: % , epno: %',employer1.name, employer1.epno;      --获取record变量的默认值。     raise info 'employer2 name: % ,epno: %', employer2.name, employer2.epno;                  --在for循环中使用record变量      for employer in select ename,empno from emp_rec order by 1  limit 1           loop                raise info 'employer name: % , epno: %', employer.name, employer.epno;          end loop;               --在select into 中使用record变量。      select ename,empno  into employer2 from emp_rec order by 1 limit 1;      raise info 'employer name: % , epno: %', employer2.name, employer2.epno;                  --在cursor中使用record变量。      OPEN C1;      FETCH C1 INTO employer2;      raise info 'employer name: % , epno: %', employer2.name, employer2.epno;      CLOSE C1;              RETURN employer.name;END;$$LANGUAGE plpgsql;--调用该函数。gaussdb=# CALL regress_record('abc');--删除函数。gaussdb=# DROP FUNCTION regress_record;--函数表达式return record示例(必须打开兼容性参数proc_outparam_override)set behavior_compat_options = 'proc_outparam_override';create or replace package pkg istype rec_type is record(c1 int, c2 int); function test1(col1 out int,col2 out int) return rec_type;end pkg;/create or replace package body pkg as  function test1(col1 out int, col2 out int) return rec_type is   r rec_type;  begin    r.c1:=300;    r.c2:=400;    col1:=100;    col2:=200;    return r;  end;end pkg;/declare  res pkg.rec_type:=pkg.rec_type();  a int;  b int;begin  res:=pkg.test1(a,b);  raise info 'a: %, b: %',a,b;  raise info '%', res;end;/-- 打开兼容性参数proc_outparam_override时,返回已定义的record类型,函数可以不需out参数create type rec_type is (c1 int, c2 int);create or replace function func(a in int) return rec_typeas declarer rec_type;beginr.c1:=1;r.c2:=2;return r;end;/call func(0);-- 打开兼容性参数proc_outparam_override时,函数直接返回未定义的record类型时,至少需要带有一个out参数create or replace function func(a in int) return recordas declaretype rc is record(c1 int);r rc;beginr.c1:=1;a:=1;return r;end;/call func(1);
support.huaweicloud.com/centralized-devg-v3-opengauss/gaussdb-12-0731.html