Oracle中insert into select和select into的区别
生活随笔
收集整理的這篇文章主要介紹了
Oracle中insert into select和select into的区别
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章轉自:http://www.linuxidc.com/Linux/2012-09/70984.htm
在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;
這里增加了原始類型的變量和記錄類型的變量,便于大家理解。
注:最后加一點,如果想創建一個和已經存在的表相同的表,可以使用如下方法:
create table test as?select * from emp;
//清除數據
truncate table test;
總結
以上是生活随笔為你收集整理的Oracle中insert into select和select into的区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Microsoft SQL Server
- 下一篇: 猫版超级玛丽 附下载