每次都查询可能会导致重复数据。
正确方式应该是打开一个Cursor,循环Cursor来插入,使用计数器来控制每次COMMIT的行数:
declare
TYPE R_CURSOR IS REF CURSOR
i number
a1_cursor R_CURSOR
a1_rowA1%ROWTYPE
begin
open a1_cursor FOR
select ID, NAME from A1
i := 0
loop
fetch a1_cursor
into a1_row
exit when a1_cursor%notfound
INSERT INTO A2 VALUES a1_row
i := i + 1
if i >= 5 then
commit
i := 0
end if
end loop
close a1_cursor
commit
end
Oracle中有三种循环(For、While、Loop):
1、loop循环:
create or replace procedure pro_test_loop isi number
begin
i:=0
loop
i:=i+1
dbms_output.put_line(i)
if i>5 then
exit
end if
end loop
end pro_test_loop
2、while循环:
create or replace procedure pro_test_loop isi number
begin
i:=0
while i<5 loop
i:=i+1
dbms_output.put_line(i)
end loop
end pro_test_loop
3、for循环1:
create or replace procedure pro_test_for isi number
begin
i:=0
for i in 1..5 loop
dbms_output.put_line(i)
end loop
end pro_test_for
4、for循环2:
create or replace procedure pro_test_cursor isuserRow t_user%rowtype
cursor userRows is
select * from t_user
begin
for userRow in userRows loop
dbms_output.put_line(userRow.Id||','||userRow.Name||','||userRows%rowcount)
end loop
end pro_test_cursor
有以下几个步骤。在表account中循环插入数据,id从1001到1005。createor
replace
procedure
test
is--存储过程,名称为test。v_id
int
--声明变量。begin。v_id
:=1001
--ACCOUNT_ID从1001插到1005。while
v_id
回答于 2022-06-06
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)