Oracle中insert into select和select into的区别

如题所述

Oracle中insert into select和select into的区别:(select into 就相当于赋值语句,insert into是复制语句),在Oracle中,将一张表的数据复制到另外一个对象中。

通常会有这两种方法:insert into select  和 select into from。前者可以将select 出来的N行(0到任意数)结果集复制一个新表中,后者只能将"一行"结果复制到一个变量中。这样说吧,select into是PL/SQL language 的赋值语句。而前者是标准的SQL语句。

做一个简单测试,我们就可以很容易地看出两者的差别。

1、首先,我们创建两个表,一个作为源表,一个作为目标表;

create table t_source(   
id number primary key,   
testname varchar2(20),   
createtime date,   
flag varchar2(10)   
);

create table t_target(   
id number primary key,   
testname varchar2(20),   
createtime date,   
flag varchar2(10)   
);  

2、接着,插入测试数据;

insert into t_source values(1,'测试数据1....1',sysdate-2,'N');   
insert into t_source values(2,'测试数据1....2',sysdate-2,'N');   
insert into t_source values(3,'测试数据1....3',sysdate-2,'N');   
commit;  
测试insert into select 操作


insert into test2 select * from t_source where id=1;   
commit;  

测试select into 操作:
因为select into是一个plsql语言中的复制语句,和:=实现的目标一样。

create or replace procedure sp_sync_test is
aa varchar2(100);
v_record t_source%rowtype;   
begin
select t1.testname into aa from t_source t1 where id = 1;
dbms_output.put_line('普通变量 t1.testname= ' || aa);

select t1.* into v_record from t_source t1 where id = 1;
dbms_output.put_line('记录变量 t1.testname= ' || v_record.testname);

end; 

3、这里增加了原始类型的变量和记录类型的变量,便于大家理解。

甲骨文股份有限公司(Oracle)是全球大型数据库软件公司,总部位于美国加州红木城的红木岸。在2008年,甲骨文股份有限公司是继Microsoft及IBM后,全球收入第三多的软件公司。Oracle数据库产品为财富排行榜上的前1000家公司所采用,许多大型网站也选用了Oracle系统。甲骨文股份有限公司于1989年正式进入中国,在北京、上海、广州和成都均设立了分支机构。

2016年1月,甲骨文表示会收购网站数据追踪服务商AddThis。2016年2月,甲骨文收购了云计算创业公司Ravello Systems。2017年6月7日发布的2017年美国《财富》500强,甲骨文公司排名第81位。2017年6月,《2017年BrandZ最具价值全球品牌100强》公布,甲骨文公司排名第46位。

参考资料

csdn博客.csdn博客[引用时间2018-1-12]

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-02-28

您好,很高兴为您解答。


insert into select可以将select 出来的N行(0到任意数)结果集复制一个新表中,select into from只能将"一行"结果复制到一个变量中。这样说吧,select into是PL/SQL language 的赋值语句。而前者是标准的SQL语句。


做一个测试看两者差别。


首先创建两个表,一个作为源表,一个作为目标表。

create table t_source(   
id number primary key,   
testname varchar2(20),   
createtime date,   
flag varchar2(10)   
);   
  
create table t_target(   
id number primary key,   
testname varchar2(20),   
createtime date,   
flag varchar2(10)   
);

 

接着,插入测试数据

insert into t_source values(1,'测试数据1....1',sysdate-2,'N');   
insert into t_source values(2,'测试数据1....2',sysdate-2,'N');   
insert into t_source values(3,'测试数据1....3',sysdate-2,'N');   
commit;

测试insert into select 操作

insert into test2 select * from t_source where id=1;   
commit;

测试select into 操作
因为select into是一个plsql语言中的复制语句,和:=实现的目标一样。

create or replace procedure sp_sync_test is  
  aa varchar2(100);   
  v_record t_source%rowtype;   
begin  
  select t1.testname into aa from t_source t1 where id = 1;   
  dbms_output.put_line('普通变量 t1.testname= ' || aa);   
  
  select t1.* into v_record from t_source t1 where id = 1;   
  dbms_output.put_line('记录变量 t1.testname= ' || v_record.testname);   
  
end;


这里增加了原始类型的变量和记录类型的变量


如若满意,请点击右侧【采纳答案】,如若还有问题,请点击【追问】


希望我的回答对您有所帮助,望采纳!


                                                                                                                             ~ O(∩_∩)O~

本回答被提问者和网友采纳
第2个回答  2015-02-13
insert into ... select
是一条SQL语句。

select ... into
是PL/SQL的一条语句。