Use of insert into select and select into in Oracle (exception: 0ra-00905: Solution of missing keyword)

Source: Internet
Author: User

The two tables are used to copy data. The most common copy statement is:

Insert into select and select into from

Note:

In Oracle, select into from cannot be used. ----- the reason is simple: select into is a value assignment statement of PL/SQL language! Oracle will throw the 0ra-00905: Missing keyword exception if you use it!

However, you can use create table select to replace this function !!! Refer to the following test code for details!

But it can be used normally in SQL Server.

First, perform a small test:

-- Create 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)
);

-- Insert 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 that the red part can automatically generate the ID sequence value)
Insert into Test2 (ID, testname, createtime, falg)
Select seq_test.nextval, t1.testname, t1.createtime, t1.falg from test1 T1;
-- Use create table select to create copied data (delete table Test2 first)
Create Table Test2 as select t1.id, t1.testname, t1.createtime, t1.falg from test1 T1;

-- Select into from is not allowed. An exception is thrown.
Select t1.id, t1.testname, t1.createtime, t1.falg into Test2 (ID, testname, createtime, falg)

From test1 T1;

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

Summary:

We recommend that you use insert into select to copy data;

If you use insert into select to generate an ID sequence value for the copy table, you must query the sequence in the SELECT statement and 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;

Typically, data is inserted into Table Test2 from Table test1. ID is inserted automatically in Table Test2. Check the code above. ID is first queried from sequence in select !!

 

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.