2010-02-12 115 views
104

我正在尝试使用Oracle做SELECT INTO。我的查询是:SELECT INTO使用Oracle

SELECT * INTO new_table FROM old_table; 

,但我得到了以下错误:

SQL Error: ORA-00905: missing keyword 
00905. 00000 - "missing keyword" 

任何想法有什么不对?


的标准行为上面应该是因为我本来以为: 但是甲骨文在自己的SQL 的方言来实现它完全不同Oracle Docs on Insert ... Select

+2

'选择into'创建一个新表是*不*标准的一部分。创建基于select的表的SQL标准是'create table .. as select ...'。在SQL标准中,“SELECT INTO”被定义为将列值读入编程语言的变量 – 2012-03-30 16:50:25

回答

222

如果NEW_TABLE已经存在,那么......

insert into new_table select * from old_table 
/

如果您想基于OLD_TABLE记录创建NEW_TABLE ...

create table new_table as select * from old_table 
/
+13

+1 @Robert:Plus,如果你只是想复制old_table的模式,请使用负的where子句,例如:create new_table as select * from old_table WHERE 1 = 2。 – 2010-02-12 09:38:55

26

select into在PL/SQL中使用设置一个变量字段值。相反,使用

create table new_table as select * from old_table 
+0

,尽管SELECT INTO是标准的一部分。 Oracle在这里做了些什么奇怪的事,或者它从未成为标准的一部分? – 2010-02-12 07:21:22

+3

'select into'是pl/sql的一部分。它是一种用于编写存储过程的语言,与sql标准没有直接关系。是的,Oracle做出了许多不属于标准的东西=) – Rorick 2010-02-12 07:45:19

+0

@RobertGould:不,“SELECT INTO”不是*标准SQL。该标准只定义了'create table .. as select ..' – 2013-11-20 22:15:46

2

用途:

create table new_table_name 
as 
select column_name,[more columns] from Existed_table; 

例如:

create table dept 
as 
select empno, ename from emp; 

如果该表已经存在:

insert into new_tablename select columns_list from Existed_table;