用mysql语句和oracle语句,分别实现查询某个表的某个字段,然后遍历判断赋值插入到另一个表中 。
mysql:
DELIMITER ??
DROP PROCEDURE IF EXISTS schema_change??
CREATE PROCEDURE schema_change()
BEGIN
DECLARE n_dpid int(11); -- 定义变量
DECLARE isInsert INTEGER DEFAULT 0; -- 定义变量
DECLARE done INT DEFAULT FALSE; -- 定义变量
DECLARE rep CURSOR FOR SELECT distinct dpid FROM t_test1; -- 游标
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN rep;
loop_1:LOOP -- 开始循环
FETCH rep INTO n_dpid;
IF done THEN
LEAVE loop_1; -- 停止循环
END IF;
SELECT COUNT(*) INTO isInsert FROM t_test2 WHERE paramgroupid = 1 and paramname = 'warn_remind' and dpid = n_dpid; -- 查询是否已经存在
IF isInsert = 0 THEN -- 不存在,则执行插入语句
insert into t_test2( paramgroupid,paramname,paramvalue,dpid,ver) values(1,'warn_remind','0',n_dpid,0);
END IF;
END LOOP;
CLOSE rep;
END??
DELIMITER ;
CALL schema_change();
oracle:
DECLARE n_count NUMBER;
n_temp NUMBER;
n_dpid NUMBER;
v_sqltext VARCHAR2 (2000);
TYPE refcur IS REF CURSOR;
cur_dpinfo refcur;
BEGIN
v_sqltext := ' SELECT distinct dpid FROM t_test1';
OPEN cur_dpinfo FOR v_sqltext; --循环数据
FETCH cur_dpinfo INTO n_dpid;
WHILE (cur_dpinfo%FOUND)
LOOP
BEGIN
SELECT COUNT (1)
INTO n_count
FROM t_test2 a
WHERE a.paramgroupid = 1 and a.paramname = 'warn_remind' and dpid = n_dpid;
IF n_count = 0 -- 判断是否已经存在数据
THEN
insert into t_test2 (id,paramgroupid,paramname,paramvalue,dpid) values((select max(id+1) id from t_test2 ),1,'warn_remind','0',n_dpid);
END IF;
END;
FETCH cur_dpinfo INTO n_dpid;
END LOOP;
CLOSE cur_dpinfo;
END;
/