Oracle中insert into select和select into的區(qū)別

oracle中,將一張表的數(shù)據(jù)復(fù)制到另外一個(gè)對(duì)象中。通常會(huì)有這兩種方法:insert into select 和 select into from。 前者可以將select 出來(lái)的N行(0到任意數(shù))結(jié)果集復(fù)制一個(gè)新表中,后者只能將一行結(jié)果復(fù)制到一個(gè)變量中。這樣說(shuō)吧,select into是PL/SQL langua

在Oracle中,將一張表的數(shù)據(jù)復(fù)制到另外一個(gè)對(duì)象中。通常會(huì)有這兩種方法:insert into select? 和 select into from。

前者可以將select 出來(lái)的N行(0到任意數(shù))結(jié)果集復(fù)制一個(gè)新表中,后者只能將”一行”結(jié)果復(fù)制到一個(gè)變量中。這樣說(shuō)吧,select into是PL/SQL language 的賦值語(yǔ)句。而前者是標(biāo)準(zhǔn)的SQL語(yǔ)句。

做一個(gè)簡(jiǎn)單測(cè)試,我們就可以很容易地看出兩者的差別。

首先,我們創(chuàng)建兩個(gè)表,一個(gè)作為源表,一個(gè)作為目標(biāo)表。

1.create table t_source(??
2.id number primary key,?? 3.testname varchar2(20),??
4.createtime date,?? 5.flag varchar2(10)??
6.);??
7.?
8.create table t_target(?? 9.id number primary key,?? 10.testname varchar2(20),??
11.createtime date,?? 12.flag varchar2(10)??
13.);?
接著,插入測(cè)試數(shù)據(jù)

1.insert into t_source values(1,’測(cè)試數(shù)據(jù)1….1′,sysdate-2,’N’);??
2.insert into t_source values(2,’測(cè)試數(shù)據(jù)1….2′,sysdate-2,’N’);?? 3.insert into t_source values(3,’測(cè)試數(shù)據(jù)1….3′,sysdate-2,’N’);?? 4.commit;? 測(cè)試insert into select 操作

1.insert into test2 select * from t_source where id=1;??
2.commit;? 測(cè)試select into 操作
因?yàn)閟elect into是一個(gè)plsql語(yǔ)言中的復(fù)制語(yǔ)句,和:=實(shí)現(xiàn)的目標(biāo)一樣。

1.create or replace procedure sp_sync_test is?
2.? aa varchar2(100);??
3.? v_record t_source%rowtype;??
4.begin? 5.? select t1.testname into aa from t_source t1 where id = 1;?? 6.? dbms_output.put_line(‘普通變量 t1.testname= ‘ || aa);?? 7.?
8.? select t1.* into v_record from t_source t1 where id = 1;?? 9.? dbms_output.put_line(‘記錄變量 t1.testname= ‘ || v_record.testname);?? 10.?
11.end;
這里增加了原始類型的變量和記錄類型的變量,便于大家理解。

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊6 分享