用mysql语句和oracle语句,分别实现查询某个表的某个字段,然后遍历判断赋值插入到另一个表中 。

用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;
/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值