当前位置:网站首页>Screen output of DB2 stored procedure, output parameters, and return result set

Screen output of DB2 stored procedure, output parameters, and return result set

2022-06-10 18:45:00 Blue black 2020

In addition to being able to process and run business logic , It can also output information and return results .

  • Screen output : Output via screen , Show the data to the user explicitly , Besides , It is also very useful when debugging stored procedures ;
  • Parameter output : adopt out Parameters , Output information , It can be displayed on the screen , It can also be accessed by other stored procedures ;
  • Return results : Implicitly return the result set , It can be displayed on the screen , It can also be accessed by other stored procedures ;

Environmental Science

  • operating system :Ubuntu 20.04
  • Db2:11.5.0.0

Screen output

You can call dbms_output Module to “ Message buffer ” The output text :

  • dbms_output.newline : Be similar to Java Of System.out.println() ;
  • dbms_output.put(xxx) : Be similar to Java Of System.out.print(xxx) ;
  • dbms_output.putline(xxx) : Be similar to Java Of System.out.println(xxx) ;

Be careful : By default, the message buffer is not “ standard output ”( That is, screen output ), therefore , You need to explicitly specify :

  • set server output on : Turn on standard output ;
  • set server output off : Turn off standard output ;

Be careful : function dbms_output.put(xxx) Does not immediately output text to the screen , Only the back one dbms_output.newline perhaps dbms_output.putline(xxx) Will cause its output .

create a file test1.sql as follows :

set serveroutput on@

begin
	call dbms_output.put_line('hello');

	call dbms_output.put('world');

	call dbms_output.put('OK');

	call dbms_output.put_line('good');

	call dbms_output.new_line();

	call dbms_output.put_line('better');
end@

set serveroutput off@

Run script test1.sql , as follows :

*  temp0602 db2 -[email protected] -f test1.sql
DB20000I  The SET SERVEROUTPUT command completed successfully.

DB20000I  The SQL command completed successfully.

hello
worldOKgood

better

DB20000I  The SET SERVEROUTPUT command completed successfully.

Screen output is very convenient for debugging scripts .

Parameter output

Call the stored procedure directly , Print its output parameters

create a file test2.sql as follows :

create or replace procedure myproc1(in p1 int, in p2 int, out p3 int)
begin
	set p3 = p1 + p2;
end@

Run script test2.sql .

Now you can call myproc1 了 ,out Parameter use ? placeholder :

*  temp0602 db2 "call myproc1(100, 50, ?)"

  Value of output parameters
  --------------------------
  Parameter Name  : P3
  Parameter Value : 150

  Return Status = 0

Accessing the output parameters returned by another stored procedure in a stored procedure

You can also call... In other stored procedures myproc1 . create a file test3.sql as follows :

set serveroutput on@

create or replace procedure myproc2()
begin
	declare p1, p2, p3 int;

	set p1 = 111;
	set p2 = 6;

	call myproc1(p1, p2, p3);

	call dbms_output.put_line('p3 = ' || p3);
end@

call [email protected]

set serveroutput off@

Run script test3.sql , as follows :

*  temp0602 db2 -[email protected] -f test3.sql         
DB20000I  The SET SERVEROUTPUT command completed successfully.

DB20000I  The SQL command completed successfully.

  Return Status = 0

p3 = 117

DB20000I  The SET SERVEROUTPUT command completed successfully.

notes : If it is called in another stored procedure myproc1 ,out Parameters can't be used ? It's occupied .

Return results

Stored procedures are not like functions , Cannot explicitly return results , However, stored procedures can implicitly return result sets .

Call the stored procedure directly , Print the result set it returns

create a file test4.sql as follows :

create or replace procedure myproc3
begin
	declare cur1 cursor with return for
		select c1, c2 from t1;

	open cur1;
end@

call [email protected]

Run script test4.sql , as follows :

*  temp0602 db2 -[email protected] -f test4.sql
DB20000I  The SQL command completed successfully.

  Result set 1
  --------------

  C1          C2         
  ----------- -----------
            1         111
            2         222
            3         333

  3 record(s) selected.

  Return Status = 0

A stored procedure can return multiple result sets . create a file test5.sql as follows :

create or replace procedure myproc4
begin
	declare cur1 cursor with return for
		select c1, c2 from t1;

	declare cur2 cursor with return for
		select c1, c2 from t2;

	open cur1;

	open cur2;
end@

call [email protected]

Run script test5.sql , as follows :

*  temp0602 db2 -[email protected] -f test5.sql
DB20000I  The SQL command completed successfully.

  Result set 1
  --------------

  C1          C2         
  ----------- -----------
            1         111
            2         222
            3         333

  3 record(s) selected.


  Result set 2
  --------------

  C1          C2        
  ----------- ----------
           11 aaa       
           22 bbb       

  2 record(s) selected.

  Return Status = 0

When defining stored procedures , You can specify the maximum number of returned result sets . such as , create a file test6.sql as follows :

create or replace procedure myproc5
	dynamic result sets 1
begin
	declare cur1 cursor with return for
		select c1, c2 from t1;

	declare cur2 cursor with return for
		select c1, c2 from t2;

	open cur1;

	open cur2;
end@

call [email protected]

Run script test6.sql , as follows :

*  temp0602 db2 -[email protected] -f test6.sql
DB20000I  The SQL command completed successfully.

  Result set 1
  --------------

  C1          C2         
  ----------- -----------
            1         111
            2         222
            3         333

  3 record(s) selected.


  Result set 2
  --------------

  C1          C2        
  ----------- ----------
           11 aaa       
           22 bbb       

  2 record(s) selected.

  Return Status = 0

SQL0464W  Procedure "DB2INST1.MYPROC5" returned "2" query result sets, which 
exceeds the defined limit "1".  SQLSTATE=0100E

*  temp0602 echo $?
2

In this case , Set... In the stored procedure dynamic result sets 1 , But it actually returns 2 A result set , So the runtime Db2 A warning message is given .

Accessing the result set returned by another stored procedure in a stored procedure

create a file test7.sql as follows :

create or replace procedure myproc6
	dynamic result sets 2
begin
	declare cur1 cursor with return for
		select c1, c2 from t1;

	declare cur2 cursor with return for
		select c1, c2 from t2;

	open cur1;

	open cur2;
end@

create or replace procedure myproc7
begin
	declare rs1, rs2 result_set_locator varying;

	declare c1, c2 int default 0;

	declare sqlcode, mysqlcode int default 0;

	call myproc6;

	associate result set locators(rs1, rs2) with procedure myproc6;

	allocate ccur1 cursor for result set rs1;

	repeat
		fetch ccur1 into c1, c2;
		set mysqlcode = sqlcode;
		call dbms_output.put_line('c1 = ' || c1 || ', c2 = ' || c2);
	until (mysqlcode <> 0)
	end repeat;

	close ccur1;
end@

set serveroutput on@

call [email protected]

set serveroutput off@

It can be seen from this example that , To access the result set returned by another stored procedure in a stored procedure , Steps are as follows :

  • declare rs1, rs2 result_set_locator varying : Definition rs1 and rs2 these two items. “ Result set locator ”;
  • call myproc6 : Calling stored procedure ;
  • associate result set locators(rs1, rs2) with procedure myproc6 : hold rs1 and rs2 Associated to a stored procedure ;
  • allocate ccur1 cursor for result set rs1 : Assign a cursor to the result set , Note that cursor variables here cannot be defined in advance , Nor can it be explicit open The cursor ;
  • fetch ccur1 into c1, c2 : Get the contents of the result set ;

Run script test7.sql , as follows :

*  temp0602 db2 -[email protected] -f test7.sql
DB20000I  The SQL command completed successfully.

DB20000I  The SQL command completed successfully.

DB20000I  The SET SERVEROUTPUT command completed successfully.

  Return Status = 0

c1 = 1, c2 = 111
c1 = 2, c2 = 222
c1 = 3, c2 = 333
c1 = 3, c2 = 333

DB20000I  The SET SERVEROUTPUT command completed successfully.

Be careful : ccur1 Although it is a cursor , But not through ccur1 is found To determine whether or not fetch To data , Will report a mistake -206, I don't understand why . We had to change it to a native way , It is judgement. fetch Of sqlcode Is it right? 0, If not 0, Just explain fetch It didn't work .

Reference resources

  • https://www.ibm.com/docs/en/db2/11.5?topic=data-developing-routines
  • https://www.ibm.com/docs/zh/db2/11.5?topic=data-developing-routines ( Chinese version )
原网站

版权声明
本文为[Blue black 2020]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101801475847.html