樣本代碼:
創(chuàng)新互聯(lián)是專業(yè)的白山網(wǎng)站建設(shè)公司,白山接單;提供網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè),網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行白山網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊,希望更多企業(yè)前來合作!
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;
注意點(diǎn):
1、SQLCODE必須要定義,且必須定義在最外層的BEGIN的下面。
2、必須要判斷SQLCODE是否等于100,等于100時退出CURSORLOOP,否則會死循環(huán)。
3、“OPEN curs”之后不要忘記“CURSORLOOP:”。