Oracle中insert into select和select into的区别

来源:互联网 发布:top域名有市场吗 编辑:程序博客网 时间:2024/04/29 19:10
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语句。


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


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


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; 
这里增加了原始类型的变量和记录类型的变量,便于大家理解。
0 0
原创粉丝点击