ITPub博客

首页 > 应用开发 > IT综合 > 我渴望的insert操作!

我渴望的insert操作!

原创 IT综合 作者:warehouse 时间:2008-12-26 22:44:21 0 删除 编辑
呵呵,都不知道起个啥名字好,这个操作我渴望已久了,但遗憾的是自己才发现,其实oracle可能一直就支持,汗,以前没有细读过tom的书,doc也看的不细...[@more@]

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/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: 事务的原子性!
请登录后发表评论 登录
全部评论

注册时间:2007-12-07

  • 博文量
    717
  • 访问量
    5366330