首页 > 应用开发 > IT综合 > 我渴望的insert操作!
SQL> desc tt7
名称 是否为空? 类型
----------------------------------------- -------- ----------------------------
ID NUMBER(38)
NAME VARCHAR2(10)
SQL> select * from tt7;
ID NAME
---------- ----------
1 a
2 a
1 a
a
a
3 A
4 A
5 B
6 b
已选择9行。
SQL> create table tt8 as select * from tt7 where 1=2;
表已创建。
SQL> edit
已写入 file afiedt.buf
1 declare
2 begin
3 for x in (select * from tt7) loop
--一直都以为oracle不支持把"行"变量直接和insert操作关联起来使用,这里"行"变量这个名字是我给起的,不知道是否准确,只要大家能理解就ok.
4 insert into tt8 values x;
5 end loop;
6* end;
SQL> /
PL/SQL 过程已成功完成。
SQL> select * from tt8;
ID NAME
---------- ----------
1 a
2 a
1 a
a
a
3 A
4 A
5 B
6 b
已选择9行。
SQL>
SQL> edit
已写入 file afiedt.buf
1* truncate table tt8
SQL> declare
2 cursor c1 is select * from tt7;
3 v_tt tt7%rowtype;
4 begin
5 open c1;
6 fetch c1 into v_tt;
7 while c1%found loop
8 insert into tt8 values v_tt;
9 fetch c1 into v_tt;
10 end loop;
11 close c1;
12 commit;
13 end;
14 /
PL/SQL 过程已成功完成。
SQL> declare
2 cursor c1 is select * from tt7;
3 type v_tt_type is record(
4 id tt7.id%type,
5 name tt7.name%type
6 );
7 v_tt v_tt_type ;
8 begin
9 open c1;
10 fetch c1 into v_tt;
11 while c1%found loop
12 insert into tt8 values v_tt;
13 fetch c1 into v_tt;
14 end loop;
15 close c1;
16 commit;
17 end;
18 /
PL/SQL 过程已成功完成。
SQL>
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/19602/viewspace-1015318/ ,如需转载,请注明出处,否则将追究法律责任。