ORACLE 存储过程循环依次查询插入
ORACLE 存储过程循环依次查询插入
刚入门oracle不久,最近有个测试的小任务,场景是模拟用户上线记录:查询用户表,然后将部分数据插入在线表中,插入一次commit一次。
先上代码:
```sql
CREATE OR REPLACE PROCEDURE SYNCBUPT IS --创建存储过程,名称是 SYNCBUPT
rn_id int; --循环次数的变量值
USERNAME VARCHAR2(50);
USERID VARCHAR2(50);
BEGIN
rn_id := 1; --从1开始到10w
while rn_id <=100000
loop
--根据rownum依次查询用户
--一次into两个变量
select FLDUSERID,FLDUSERID into USERID,USERNAME from (select ROWNUM rn,a.* from TBLREGISTERUSERS a)b where b.rn = rn_id;
INSERT INTO TBLONLINEUSERS1(FLDUSERID, FLDSUBCONTROLID, FLDMAINCONTROLID, FLDLOGINDATE, FLDEXTEND, FLDUSERIP, FLDUSERMAC, FLDREADFLAG, FLDVPNIP, FLDSOUIP, FLDSESSIONID, FLDHOSTNAME, FLDDNSIP, FLDDHCPENABLE, FLDOSFLAG, FLDUSERIP1, FLDUSERNAME, FLDVLANID, FLDPVLANID, FLDPHONEFLAG) VALUES (USERID, NULL, '3',SYSDATE, NULL, '172.28.4.106', 'C8600050D335', '0', '0.0.0.0', '0.0.0.0', '7124', NULL, NULL, '0', '0', NULL, USERNAME, '697', '2401', '0');
commit;
--每次操作完给变量加1
rn_id := rn_id +1;
end loop;
END;
我用的操作是用一个变量去接受一个10w查询的select,然后使用这个变量来开启循环,但这样做不符合测试要求:
FOR CS IN (SELECT XH, XM, XY, BJ, NJ, ZY, SFZH
FROM BZKS_INFO
ORDER BY XH) LOOP
思路主要来自这篇文章https://blog.csdn.net/gao_xiao_qi/article/details/102508671