您好,登錄后才能下訂單哦!
樣本代碼:
DROP PROCEDURE QUOTATION.COPY_SAMPLE;
CREATE PROCEDURE QUOTATION.COPY_SAMPLE (
IN tableNameFrom VARCHAR(30)
, IN tableNameTo VARCHAR(30)
, INOUT copyResult INTEGER)
BEGIN
DECLARE SQLCODE INTEGER DEFAULT 0;
SET copyResult = 0;
-- Proecss 1
BEGIN
DECLARE fromSql VARCHAR(32672);
DECLARE toSql VARCHAR(32672);
DECLARE seqTo VARCHAR(30);
DECLARE templateParserId INTEGER;
DECLARE uuid VARCHAR(36);
DECLARE stmt STATEMENT;
DECLARE curs CURSOR FOR stmt;
SET seqTo = 'SEQ_' || tableNameTo;
SET fromSql = 'SELECT MAX(TEMPLATE_PARSER_ID), UUID FROM QUOTATION.' || tableNameFrom || ' GROUP BY UUID';
PREPARE stmt FROM fromSql;
OPEN curs;
CURSORLOOP:
LOOP
FETCH curs INTO templateParserId, uuid;
-- Do nothing if no data or processed all datas.
IF SQLCODE = 100 THEN LEAVE CURSORLOOP;
END IF;
SET uuid = (SELECT CONCAT(HEX(RAND()), HEX(RAND())) FROM SYSIBM.SYSDUMMY1);
SET toSql = 'INSERT INTO QUOTATION.' || tableNameTo || ' (TEMPLATE_PARSER_ID, UUID) VALUES (NEXTVAL FOR QUOTATION.' || seqTo || ',''' || uuid || ''')';
PREPARE s FROM toSql;
EXECUTE s;
END LOOP;
CLOSE curs;
END;
-- Proecss 2
BEGIN
-- ......
END;
SET copyResult = 1;
END;
注意點:
1、SQLCODE必須要定義,且必須定義在最外層的BEGIN的下面。
2、必須要判斷SQLCODE是否等于100,等于100時退出CURSORLOOP,否則會死循環。
3、“OPEN curs”之后不要忘記“CURSORLOOP:”。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。