SQL 使用 Oracle 的 SELECT INTO

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2250196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 05:22:24  来源:igfitidea点击:

SELECT INTO using Oracle

sqloracleora-00905

提问by Robert Gould

I'm trying to do a SELECT INTO using Oracle. My query is:

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

SELECT * INTO new_table FROM old_table;

But I get the following error:

但我收到以下错误:

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

Any ideas what's wrong?

任何想法有什么问题?



The Standard behavior of the above should be as I originally thought: However Oracle implemented it totally differently in their own dialect of SQL Oracle Docs on Insert ... Select

上面的标准行为应该和我最初的想法一样:但是 Oracle 在他们自己的 SQL Oracle Docs on Insert ... Select方言中完全不同地实现了它

回答by APC

If NEW_TABLE already exists then ...

如果 NEW_TABLE 已经存在,则 ...

insert into new_table 
select * from old_table
/

If you want to create NEW_TABLE based on the records in OLD_TABLE ...

如果要根据 OLD_TABLE 中的记录创建 NEW_TABLE ...

create table new_table as 
select * from old_table
/

If the purpose is to create a new but empty table then use a WHERE clause with a condition which can never be true:

如果目的是创建一个新的空表,则使用 WHERE 子句,其条件永远不会为真:

create table new_table as 
select * from old_table
where 1 = 2
/

Remember that CREATE TABLE ... AS SELECT creates only a table with the same projection as the source table. The new table does not have any constraints, triggers or indexes which the original table might have. Those still have to be added manually (if they are required).

请记住, CREATE TABLE ... AS SELECT 仅创建一个与源表具有相同投影的表。新表没有原始表可能具有的任何约束、触发器或索引。那些仍然必须手动添加(如果需要)。

回答by wallyk

select intois used in pl/sql to set a variable to field values. Instead, use

select into在 pl/sql 中用于将变量设置为字段值。相反,使用

create table new_table as select * from old_table

回答by PRADEEP R

Use:

用:

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

Example:

例子:

create table dept
as
select empno, ename from emp;

If the table already exists:

如果表已经存在:

insert into new_tablename select columns_list from Existed_table;