oracle sql语法兑现表数据拷贝,更新

oracle sql语法实现表数据拷贝,更新
一、oracle SELECT INTO 和 INSERT INTO SELECT 两种表复制语句详解

1.INSERT INTO SELECT语句

语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Table1

注意:(1)要求目标表Table2必须存在,并且字段field,field2...也必须存在

(2)注意Table2的主键约束,如果Table2有主键而且不为空,则 field1, field2...中必须包括主键

(3)注意语法,不要加values,和插入一条数据的sql混了,不要写成:

Insert into Table2(field1,field2,...) values (select value1,value2,... from Table1)

由于目标表Table2已经存在,所以我们除了插入源表Table1的字段外,还可以插入常量。

2.SELECT INTO FROM语句

语句形式为:SELECT vale1, value2 into Table2 from Table1

要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中。

select a,c INTO Table2 from Table1 

:如果在sql/plus或者PL/SQL执行这条语句,会报"ORA-00905:缺失关键字"错误,原因是PL/Sql与T-SQL的区别。
T-SQL中该句正常,但PL/SQL中解释是:
select..into is part of PL/SQL language which means you have to use it inside a PL/SQL block. You can not use it in a SQL statement outside of PL/SQL.
即不能单独作为一条sql语句执行,一般在PL/SQL程序块(block)中使用。
如果想在PL/SQL中实现该功能,可使用Create table newTable as select * from ...:
如: create table NewTable as select * from ATable;

NewTable 除了没有键,其他的和ATable一样
eg:
SELECT LastName,Firstname INTO Persons_backup FROM Persons WHERE City='Beijing'

SELECT Persons.LastName,Orders.OrderNo INTO Persons_Order_Backup
FROM Persons INNER JOIN Orders ON Persons.Id_P=Orders.Id_P

二、Oracle中merge into的使用

•1,可省略的update 或者insert
•   MERGE INTO products p  
•    2 USING newproducts np  
•    3 ON (p.product_id = np.product_id)  
•    4 WHEN MATCHED THEN 
•    5 UPDATE 
•    6 SET p.product_name = np.product_name,  
•    7 p.category = np.category;
  使用表newproducts中的product_name 和category字段来更新表products 中相同product_id的product_name 和category.

2,当条件不满足的时候把newproducts表中的数据INSERT 到表products中。

•MERGE INTO products p  
•    USING newproducts np  
•     ON (p.product_id = np.product_id)  
•     WHEN NOT MATCHED THEN 
•     INSERT 
•     VALUES (np.product_id, np.product_name,  
•     np.category);  
3,带条件的insert 和update

•MERGE INTO products p  
• USING newproducts np  
•   ON (p.product_id = np.product_id)  
•   WHEN MATCHED THEN 
•   UPDATE 
•   SET p.product_name = np.product_name  
•   WHERE p.category = np.category;
insert 和update 都带有where 字句



•MERGE INTO products p  
• USING newproducts np  
•    ON (p.product_id = np.product_id)  
•    WHEN MATCHED THEN 
•    UPDATE 
•     SET p.product_name = np.product_name,  
•     p.category = np.category  
•    WHERE p.category = 'DVD' 
•    WHEN NOT MATCHED THEN 
•     INSERT 
•     VALUES (np.product_id, np.product_name, np.category)  
•    WHERE np.category != 'BOOKS' 
4,无条件的insert

•MERGE INTO products p  
• USING newproducts np  
•  ON (1=0)  
•  WHEN NOT MATCHED THEN 
•   INSERT 
•   VALUES (np.product_id, np.product_name, np.category)  
•   WHERE np.category = 'BOOKS' 


5,delete 子句

1  merge into products p
  2  using newproducts np
  3  on(p.product_id = np.product_id)
  4  when matched then
  5  update
  6  set p.product_name = np.product_name
  7  delete where category = 'macle1_cate';


动机:

想在Oracle中用一条SQL语句直接进行Insert/Update的操作。

说明:

在进行SQL语句编写时,我们经常会遇到大量的同时进行Insert/Update的语句 ,也就是说当存在记录时,就更新(Update),不存在数据时,就插入(Insert)。


参考
http://www.cnblogs.com/highriver/archive/2011/08/02/2125043.html