我渴望的insert操作!

warehouse發表於2008-12-26
呵呵,都不知道起個啥名字好,這個操作我渴望已久了,但遺憾的是自己才發現,其實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部落格 ” ,連結:http://blog.itpub.net/19602/viewspace-1015318/,如需轉載,請註明出處,否則將追究法律責任。

相關文章