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 !!