--MYSQL存储过程返回多行。
1.表数据准备
drop table if exists t1;
drop table if exists t2;
drop table if exists t3;
create table t1(id varchar(20),data int )default charset gbk;
create table t2(id int) default charset gbk;
create table t3(id varchar(20),c int)default charset gbk;
insert into t1 values ('xsq',1),('xsq2',2),('xsq3',3),('薛双奇',4),('北京',5);
insert into t2 values (1),(2),(3),(4);
select * from t1;
select * from t2;
select * from t3;
2.创建存储过程。
drop PROCEDURE curdemo;
delimiter //
CREATE PROCEDURE curdemo()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE a CHAR(16);
DECLARE b,c INT;
DECLARE cur1 CURSOR FOR SELECT id,data FROM sjzt.t1;
DECLARE cur2 CURSOR FOR SELECT id FROM sjzt.t2;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
OPEN cur1;
OPEN cur2;
truncate table sjzt.t3;
REPEAT FETCH cur1 INTO a, b;
FETCH cur2 INTO c;
IF NOT done THEN
IF b < c THEN
INSERT INTO sjzt.t3 VALUES (a,b);
ELSE
INSERT INTO sjzt.t3 VALUES (a,c);
END IF;
END IF;
UNTIL done
END REPEAT;
CLOSE cur1;
CLOSE cur2;
DROP TABLE IF EXISTS TEMP_T3;
CREATE TEMPORARY TABLE TEMP_T3(ID VARCHAR(20),C INT);
INSERT INTO TEMP_T3 SELECT * FROM T3;
SELECT * FROM TEMP_T3;
END;
//
3.调用存储过程。
call curdemo();
MySQL [sjzt]> call curdemo();
-> //
+-----------+------+
| ID | C |
+-----------+------+
| xsq | 1 |
| xsq2 | 2 |
| xsq3 | 3 |
| 薛双奇 | 4 |
+-----------+------+
4 rows in set (0.78 sec)
4.帆软前端调用存储过程。
(1)添加数据库模板
2)预览存储过程,会执行并输出存储过程。可以发现会返回多行。
3)创建数据库连接。
由于OB不支持UTF8字符集,支持GBK和UTF8MB4。而帆软支持GBK和UTF8,所以如果有中文,必须设置为GBK字符集才能正常显示。
数据库连接。连接OB就当做MySQL连接,只是连接时需要注意用户部分,
填写的是:用户名@租户名#集群名称。
数据库:MySQL
驱动:com.mysql.jdbc.Driver
主机:192.168.1.61
端口:2883 --OBPROXY 代理服务端口是2883;
数据库:sjzt --sjzt用户登录到sjzt数据库
用户:sjzt@tent_t1#hjzqobce --hjzqobce集群下 tent_t1租户(实例)下 sjzt用户登录。
密码:sjzt --数据中台用户的密码:sjzt
编码:GBK --字符集设置为GBK
URL:jdbc:mysql://192.168.1.61:2883/sjzt
hjzqobce:集群名称。
tent_t1:租户名称
连接串解释:
hjzqobce集群下 tent_t1租户里面 sjzt用户通过2883端口登录到sjzt 数据库,登录密码是:sjzt
mysql -h192.168.1.61 -usjzt@tent_t1#hjzqobce -P2883 -p'sjzt' -c -A sjzt
[admin@obcontrol ~]$ mysql -h192.168.1.61 -usjzt@tent_t1#hjzqobce -P2883 -p'sjzt' -c -A sjzt
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 39
Server version: 5.6.25 OceanBase_CE 4.0.0.0 (r103000022023011215-05bbad0279302d7274e1b5ab79323a2c915c1981) (Built Jan 12 2023 15:28:27)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [sjzt]>
MySQL [sjzt]>
MySQL [sjzt]> show tables;
+----------------+
| Tables_in_sjzt |
+----------------+
| emp |
| myt1 |
| myt2 |
| t1 |
| t2 |
| t2_m_rcr |
| t3 |
| t_m_rclc |
| t_m_rcrc |
| tbl1 |
| tbl1_h |
| tbl1_k |
| tbl1_l |
| tbl1_lc |
| tbl1_log_rc |
| tbl1_r |
| tbl2 |
| tbl3 |
| tbl4 |
| tbl5 |
| tbl6 |
| tbl7 |
| tbl_rr |
| test |
| test1 |
| test2 |
| test3 |
| test4 |
| ttgh |
| ttgh2 |
| v_emp |
+----------------+
31 rows in set (0.01 sec)
5.总结
如果使用帆软展示OB4.0.的数据,且要执行存储过程,则需要将OB的表设置为GBK字符集,否则中文乱码。
OB存储过程支持游标,临时表,返回多行数据。