Help Center/
GaussDB/
Developer Guide(Centralized_3.x)/
Stored Procedure/
Arrays, Collections, and Records/
Records
Updated on 2024-05-07 GMT+08:00
Records
record Variables
Perform the following operations to create a record variable:
Define a record type and use this type to declare a variable.
Syntax
For the syntax of the record type, see Figure 1.
The above syntax diagram is explained as follows:
- record_type: record type
- field: record columns
- datatype: record data type
- expression: expression for setting a default value
In GaussDB:
- When assigning values to record variables, you can:
- Declare a record type and define member variables of this type when you declare a function or stored procedure.
- Assign the value of a record variable to another record variable.
- Use SELECT INTO or FETCH to assign values to a record type.
- Assign the NULL value to a record variable.
- The INSERT and UPDATE statements cannot use a record variable to insert or update data.
- The constructor of the record type cannot be used as the default value of a function or stored procedure parameter.
- When package_name.record_type is used to declare a record variable, the default value of the record type becomes invalid. Therefore, you are advised not to use package_name.record_type to declare a record variable when the record type has a default value.
- Just like a variable, a record column of the compound type does not have a default value in the declaration.
- If a member has a record type, the default value of the inner record type is not transferred to the outer record type.
- If enable_recordtype_check_strict is set to on and the member is of the record type and a column of the record type has the not null or default attribute, an error is reported during stored procedure or package compilation.
- If enable_recordtype_check_strict is set to on, a package contains the record type, and a column of the record type has the not null or default attribute, an error is reported during creation or compilation.
- If enable_recordtype_check_strict is set to on, the not null function of the record type in the stored procedure takes effect.
- The data type can be the record type, array type, or collection type defined in a stored procedure (anonymous blocks are not supported).
- After the GUC parameter set behavior_compat_options is set to 'proc_outparam_override':
- Functions that contain the out output parameter and returns data of the record type can be invoked using SELECT and CALL in external SQL statements, and can be invoked using PERFORM and expressions in stored procedures.
- If the function returns data of the undefined record type, at least one out parameter is required. If the function returns data of the defined record type, the out parameter is optional. For details, see the example.
- Functions that contain the out output parameter and return data of the composite or record type, ensure that the expected return type of the function is the same as the actual return type.
Examples
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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
The table definition used in the following is defined as follows: 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) | -- Perform array operations in the function. gaussdb=# CREATE OR REPLACE FUNCTION regress_record(p_w VARCHAR2) RETURNS VARCHAR2 AS $$ DECLARE -- Declare a record type. type rec_type is record (name varchar2(100), epno int); employer rec_type; -- Use %type to declare the record type. type rec_type1 is record (name emp_rec.ename%type, epno int not null :=10); employer1 rec_type1; -- Declare a record type with a default value. 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 -- Assign a value to a member record variable. employer.name := 'WARD'; employer.epno = 18; raise info 'employer name: % , epno:%', employer.name, employer.epno; -- Assign the value of a record variable to another variable. employer1 := employer; raise info 'employer1 name: % , epno: %',employer1.name, employer1.epno; -- Assign the NULL value to a record variable. employer1 := NULL; raise info 'employer1 name: % , epno: %',employer1.name, employer1.epno; -- Obtain the default value of a record variable. raise info 'employer2 name: % ,epno: %', employer2.name, employer2.epno; -- Use a record variable in the FOR loop. 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; -- Use a record variable in the SELECT INTO statement. select ename,empno into employer2 from emp_rec order by 1 limit 1; raise info 'employer name: % , epno: %', employer2.name, employer2.epno; -- Use a record variable in a cursor. OPEN C1; FETCH C1 INTO employer2; raise info 'employer name: % , epno: %', employer2.name, employer2.epno; CLOSE C1; RETURN employer.name; END; $$ LANGUAGE plpgsql; -- Call this function. gaussdb=# CALL regress_record('abc'); -- Delete the function. gaussdb=# DROP FUNCTION regress_record; -- Example of the function expression return record (the compatibility parameter proc_outparam_override must be enabled) set behavior_compat_options = 'proc_outparam_override'; create or replace package pkg is type 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; / -- If the compatibility parameter proc_outparam_override is enabled, the data of the defined record type is returned. The out parameter is not required for the function. create type rec_type is (c1 int, c2 int); create or replace function func(a in int) return rec_type as declare r rec_type; begin r.c1:=1; r.c2:=2; return r; end; / call func(0); -- If the compatibility parameter proc_outparam_override is enabled, the data of the undefined record type is returned and at least one out parameter is required for the function create or replace function func(a in int) return record as declare type rc is record(c1 int); r rc; begin r.c1:=1; a:=1; return r; end; / call func(1); |
Parent topic: Arrays, Collections, and Records
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
The system is busy. Please try again later.
For any further questions, feel free to contact us through the chatbot.
Chatbot