RETURN Statements
GaussDB(DWS) provides two methods for returning data: RETURN (or RETURN NEXT) and RETURN QUERY. RETURN NEXT and RETURN QUERY are used only for functions and cannot be used for stored procedures.
RETURN
Syntax
Figure 1 shows the syntax of a return statement.
The syntax is explained as follows:
This statement returns control from a stored procedure or function to a caller.
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 |
-- Create the stored procedure proc_staffs: CREATE OR REPLACE PROCEDURE proc_staffs ( section NUMBER(6), salary_sum out NUMBER(8,2), staffs_count out INTEGER ) IS BEGIN SELECT sum(salary), count(*) INTO salary_sum, staffs_count FROM staffs where section_id = section; END; / -- Create the stored procedure proc_return: CREATE OR REPLACE PROCEDURE proc_return AS v_num NUMBER(8,2); v_sum INTEGER; BEGIN proc_staffs(30, v_sum, v_num); --Call a statement. dbms_output.put_line(v_sum||'#'||v_num); RETURN; --Return a statement. END; / -- Invoke a stored procedure proc_return: CALL proc_return(); -- Delete a stored procedure: DROP PROCEDURE proc_staffs; DROP PROCEDURE proc_return; --Create the function func_return. CREATE OR REPLACE FUNCTION func_return returns void language plpgsql AS $$ DECLARE v_num INTEGER := 1; BEGIN dbms_output.put_line(v_num); RETURN; --Return a statement. END $$; -- Invoke the function func_return. CALL func_return(); 1 -- Delete the function: DROP FUNCTION func_return; |
RETURN NEXT and RETURN QUERY
Syntax
When creating a function, specify SETOF datatype for the return values.
return_next_clause::=
return_query_clause::=
The syntax is explained as follows:
If a function needs to return a result set, use RETURN NEXT or RETURN QUERY to add results to the result set, and then continue to execute the next statement of the function. As the RETURN NEXT or RETURN QUERY statement is executed repeatedly, more and more results will be added to the result set. After the function is executed, all results are returned.
RETURN NEXT can be used for scalar and compound data types.
RETURN QUERY has a variant RETURN QUERY EXECUTE. You can add dynamic queries and add parameters to the queries by using USING.
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 |
CREATE TABLE t1(a int); INSERT INTO t1 VALUES(1),(10); --RETURN NEXT CREATE OR REPLACE FUNCTION fun_for_return_next() RETURNS SETOF t1 AS $$ DECLARE r t1%ROWTYPE; BEGIN FOR r IN select * from t1 LOOP RETURN NEXT r; END LOOP; RETURN; END; $$ LANGUAGE PLPGSQL; call fun_for_return_next(); a --- 1 10 (2 rows) -- RETURN QUERY CREATE OR REPLACE FUNCTION fun_for_return_query() RETURNS SETOF t1 AS $$ DECLARE r t1%ROWTYPE; BEGIN RETURN QUERY select * from t1; END; $$ language plpgsql; call fun_for_return_next(); a --- 1 10 (2 rows) |
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.
For any further questions, feel free to contact us through the chatbot.
Chatbot