Use of INSERT into select and select INTO in Oracle (RPM)

Source: Internet
Author: User
Tags throw exception

Original address: Http://hi.baidu.com/huahua035/item/87d5e71e6a7d31f187ad4ea5

Two tables make copies of the data, the most commonly used copy statements are:

INSERT into select and select into from

But please be absolutely aware of:

In Oracle, select into from is not available-----reason is simple: SELECT INTO is a PL/SQL language assignment Statement! If used, Oracle throws 0ra-00905:missing keyword exceptions!

However, you can replace this function with the CREATE TABLE Select!!! For specific reference test code below!

However, it can be used normally in SQL Server.

Let's do a little test first:

--Build a table
CREATE TABLE Test1 (
ID number primary Key,
TestName varchar2 (20),
Createtime date,
Falg VARCHAR2 (10)
);

CREATE TABLE Test2 (
ID number primary Key,
TestName varchar2 (20),
Createtime date,
Falg VARCHAR2 (10)
);

--Inserting test data
INSERT into test1 values (1, ' test data 1 .... 1 ', sysdate-2, ' N ');
INSERT into test1 values (2, ' test data 1 .... 2 ', sysdate-2, ' N ');
INSERT into test1 values (3, ' test data 1 .... 3 ', sysdate-2, ' N ');
Commit
--Use INSERT into Select to copy data (note the Red section, you can automatically generate ID sequence values)
Insert into Test2 (ID,TESTNAME,CREATETIME,FALG)
Select Seq_test.nextval,t1.testname,t1.createtime,t1.falg from Test1 T1;
--Creating the copied data using the CREATE TABLE select (Note to delete the Test2 table first)
CREATE TABLE Test2 as select T1.id,t1.testname,t1.createtime,t1.falg from Test1 T1;

--select into from can not, throw exception
Select T1.id,t1.testname,t1.createtime,t1.falg into Test2 (ID,TESTNAME,CREATETIME,FALG)
from Test1 T1;

--Test use of the SELECT INTO assignment statement in PL/SQL language
Create or replace procedure Test1_prod
Is
AA varchar2 (100);
Begin
Select T1.testname to AA from test1 T1 where id=1;
Dbms_output.put_line (' t1.testname= ' | | aa);
End

Summarize:

Data copy, insert into select is recommended;

When using INSERT into SELECT, if the ID sequence value is generated for the copy table, it needs to be queried from the sequence in the Select to insert the Copy table, for example:

Insert into Test2 (ID,TESTNAME,CREATETIME,FALG)
Select Seq_test.nextval,t1.testname,t1.createtime,t1.falg from Test1 T1;

Typical from the Test1 table query out the data inserted into the Test2 table, test2 to automatically insert ID, see the above code, ID to select in the first query from the sequence!!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.