当前位置:网站首页>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
outParameters , 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 OfSystem.out.println();dbms_output.put(xxx): Be similar to Java OfSystem.out.print(xxx);dbms_output.putline(xxx): Be similar to Java OfSystem.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: Definitionrs1andrs2these two items. “ Result set locator ”;call myproc6: Calling stored procedure ;associate result set locators(rs1, rs2) with procedure myproc6: holdrs1andrs2Associated 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 )
边栏推荐
- ETL的使用过程中遇到的坑(ETL中文乱码)
- 滑动窗口最值问题
- 元数据管理,数字化时代企业的基础建设
- [QNX hypervisor 2.2 user manual] 3.3 configure guest
- 记录一个超级乌龙的智障bug,也许能帮助类似我的白吃
- Common methods of stream flow lambder
- C language -- 13 loop statement while
- Developers changing the world - Yao Guang teenagers playing Tetris
- Domestic cosmetics, lost 618
- Data URL
猜你喜欢

Uniapp native JS to convert the Gregorian calendar to the lunar calendar

自定义类型:结构体

连续六年稳居中国SDN(软件)市场份额第一

【数据库】结构化数据、非结构化数据、半结构化数据的区别

Vcsa7u3c installation tutorial

两部门发文明确校外培训机构消防安全条件

c语言---3 初识变量

The question of enterprise managers, where have we spent our money after so many years of informatization?

Wechat applet, get the current page and judge whether the current page is a tabbar page
![[CEPH] CEPH configuration source code analysis | common/config*](/img/d6/ba8d3ccc28d11e7fd9662b730fd998.jpg)
[CEPH] CEPH configuration source code analysis | common/config*
随机推荐
JS Touch
Salesmartly | add a new channel slack to help you close the customer relationship
Three ways generated by stream lambda
商业智能BI的服务对象,企业管理者的管理“欲望”该如何实现?
低碳数据中心建设思路及未来趋势
Vcsa7u3c installation tutorial
企业管理者的质疑,这么多年的信息化,我们的钱花哪去了?
Win7系统下无法正常安装JLINK CDC UART驱动的问题解决
In the digital age, why should enterprises make digital transformation?
一道题讲解动态规划的前世今生
三部曲解下棋先手后手问题
微信小程序,获取当前页面,判断当前页面是不是tabbar页面
连续六年稳居中国SDN(软件)市场份额第一
2021年全球十大模拟IC供应商:德州仪器稳居第一,Skyworks营收增幅最高
作为程序员,对于底层原理真的有那么重要吗?
数字化时代,企业如何进行数据安全治理,保障数据资产安全
【QNX Hypervisor 2.2 用户手册】3.2.2 VM配置示例
MySQL索引失效场景
换根呀呀啊呀
Linked List