如何将存储过程产生的游标/结果集插入到db2中的临时表中

时间:2019-04-12 02:56:05

标签: stored-procedures db2 resultset

您好,我尝试使用此方法将结果集插入到临时表中,

这是在How i can put a result set from a stored procedure in a temporary table in DB2之前被问到的,但没有人回答。

CREATE OR REPLACE PROCEDURE SCHEMANAME.SP_CALLER()
DYNAMIC RESULT SETS 1

BEGIN
DECLARE GLOBAL TEMPORARY TABLE SESSION.temp_table(
useraccountid SMALLINT,
roleid SMALLINT,
userid varchar(20),
username varchar(50),
lastlogindate TIMESTAMP,
deleted SMALLINT
) ON COMMIT PRESERVE ROWS;

P1:BEGIN

DECLARE loc_cursor RESULT_SET_LOCATOR VARYING;

CALL SCHEMANAME.PR_USERACCOUNTGETALL();


ASSOCIATE RESULT SET LOCATOR (loc_cursor) WITH PROCEDURE SCHEMANAME.PR_USERACCOUNTGETALL;
ALLOCATE cur CURSOR FOR RESULT SET loc_cursor;

--INSERT INTO #TEMPTABLE (cur);
--FETCH cur INTO TRANSACTIONCODE;
--CLOSE CUR;

Insert into session.temp_table(cur);

END P1;

END

更新的代码:

我尝试了您建议的代码段,它没有错误,但是我需要返回从SESSION.temp_table查询的结果集,

使用SELECT * FROM SESSION.temp_table来返回带有选择的返回的光标,其中userid ='JOHNDOE'; 打开ret;

我现在如何从这里返回值?

CREATE OR REPLACE PROCEDURE ITRS.SP_CALLER()
DYNAMIC RESULT SETS 1

BEGIN
DECLARE GLOBAL TEMPORARY TABLE SESSION.temp_table(
USERACCOUNTID SMALLINT,
ROLEID SMALLINT,
USERID VARCHAR(20),
USERNAME VARCHAR(50),
LASTLOGINDATE TIMESTAMP,
DELETED SMALLINT
);

P1:BEGIN

DECLARE loc_cursor RESULT_SET_LOCATOR VARYING;
DECLARE a CHAR(5) DEFAULT '00000';
DECLARE v_useraccountid SMALLINT;
DECLARE v_roleid SMALLINT;
DECLARE v_userid VARCHAR(20);
DECLARE v_username VARCHAR(50);
DECLARE v_lastlogindate TIMESTAMP;
DECLARE v_deleted SMALLINT;

DECLARE ret CURSOR WITH RETURN FOR
SELECT * FROM SESSION.temp_table where userid='JOHNDOE';
open ret;


CALL ITRS.PR_USERACCOUNTGETALL();

ASSOCIATE RESULT SET LOCATOR (loc_cursor) WITH PROCEDURE ITRS.PR_USERACCOUNTGETALL;
ALLOCATE cur CURSOR FOR RESULT SET loc_cursor;

ins_loop: LOOP
FETCH cur INTO v_useraccountid,v_roleid,v_userid,v_username,v_lastlogindate,v_deleted;
IF a <> '00000' THEN LEAVE ins_loop; END IF;
INSERT INTO SESSION.temp_table(USERACCOUNTID,ROLEID,USERID,USERNAME,LASTLOGINDATE,DELETED)
    VALUES(v_useraccountid,v_roleid,v_userid,v_username,v_lastlogindate,v_deleted);

END LOOP ins_loop;

CLOSE cur;
END P1;

END

已更新:

在这里,我尝试通过SP正常插入表中的数据,因此我删除了临时表的声明,而直接将可哑表名替换为临时表

--#SET TERMINATOR @
CREATE OR REPLACE PROCEDURE ITRS.SP_CALLER()
DYNAMIC RESULT SETS 1

BEGIN

DECLARE loc_cursor RESULT_SET_LOCATOR VARYING;
DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE v_useraccountid SMALLINT;
DECLARE v_roleid SMALLINT;
DECLARE v_userid VARCHAR(20);
DECLARE v_username VARCHAR(50);
DECLARE v_lastlogindate TIMESTAMP;
DECLARE v_deleted SMALLINT;

DECLARE ret CURSOR WITH RETURN FOR
SELECT * FROM ITRS.DUMBTABLE where USERID='JOHNDOE';


CALL ITRS.PR_USERACCOUNTGETALL();

ASSOCIATE RESULT SET LOCATOR (loc_cursor) WITH PROCEDURE ITRS.PR_USERACCOUNTGETALL;
ALLOCATE cur CURSOR FOR RESULT SET loc_cursor;

ins_loop: 
LOOP
  FETCH cur INTO v_useraccountid,v_roleid,v_userid,v_username,v_lastlogindate,v_deleted;
  IF SQLSTATE <> '00000' THEN LEAVE ins_loop; END IF;
  INSERT INTO ITRS.DUMBTABLE(USERACCOUNTID,ROLEID,USERID,USERNAME,LASTLOGINDATE,DELETED)
  VALUES(v_useraccountid,v_roleid,v_userid,v_username,v_lastlogindate,v_deleted);
  COMMIT;
END LOOP ins_loop;
CLOSE cur;
open ret;
END@

1 个答案:

答案 0 :(得分:1)

例如,您应按LOOP statement in SQL procedures中所述循环从游标中获取数据。

--#SET TERMINATOR @
DECLARE GLOBAL TEMPORARY TABLE SESSION.temp_table(
USERACCOUNTID SMALLINT,
ROLEID SMALLINT,
USERID VARCHAR(20),
USERNAME VARCHAR(50),
LASTLOGINDATE TIMESTAMP,
DELETED SMALLINT
) WITH REPLACE NOT LOGGED@

CREATE OR REPLACE PROCEDURE ITRS.SP_CALLER()
DYNAMIC RESULT SETS 1

BEGIN

DECLARE loc_cursor RESULT_SET_LOCATOR VARYING;
DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE v_useraccountid SMALLINT;
DECLARE v_roleid SMALLINT;
DECLARE v_userid VARCHAR(20);
DECLARE v_username VARCHAR(50);
DECLARE v_lastlogindate TIMESTAMP;
DECLARE v_deleted SMALLINT;

DECLARE ret CURSOR WITH RETURN FOR
SELECT * FROM SESSION.temp_table where userid='JOHNDOE';

DECLARE GLOBAL TEMPORARY TABLE SESSION.temp_table(
USERACCOUNTID SMALLINT,
ROLEID SMALLINT,
USERID VARCHAR(20),
USERNAME VARCHAR(50),
LASTLOGINDATE TIMESTAMP,
DELETED SMALLINT
) WITH REPLACE NOT LOGGED;

CALL ITRS.PR_USERACCOUNTGETALL();

ASSOCIATE RESULT SET LOCATOR (loc_cursor) WITH PROCEDURE ITRS.PR_USERACCOUNTGETALL;
ALLOCATE cur CURSOR FOR RESULT SET loc_cursor;

ins_loop: 
LOOP
  FETCH cur INTO v_useraccountid,v_roleid,v_userid,v_username,v_lastlogindate,v_deleted;
  IF SQLSTATE <> '00000' THEN LEAVE ins_loop; END IF;
  INSERT INTO SESSION.temp_table(USERACCOUNTID,ROLEID,USERID,USERNAME,LASTLOGINDATE,DELETED)
  VALUES(v_useraccountid,v_roleid,v_userid,v_username,v_lastlogindate,v_deleted);
END LOOP ins_loop;

CLOSE cur;
open ret;

END@

请注意,如果使用 COMPOUND SQL语句,则必须使用一些不同的语句分隔符,例如@,而不是默认的;
请勿更改SQLSTATE变量名。这是特殊的。几乎在每条语句自动执行后设置。
如果运行包含此文本的文件,则该示例应在Db2命令行处理器中工作(它理解第一行中的指令)。
如果您使用其他工具来运行该语句,则必须找到一个可以更改默认语句终止符的地方。