我正在尝试使用 Oracle 执行 SELECT INTO。我的查询是:
SELECT * INTO new_table FROM old_table;
但我收到以下错误:
SQL Error: ORA-00905: missing keyword
00905. 00000 - "missing keyword"
有什么想法有什么问题吗?
上面的标准行为应该和我最初想的一样:但是 Oracle 在他们自己的 SQL 方言中完全不同地实现了它Oracle Docs on Insert ... Select
select into
创建新表不是标准的一部分。基于选择创建表的 SQL 标准是 create table .. as select ...
。在 SQL 标准中 SELECT INTO
被定义为将列值读入编程语言中的变量
如果 NEW_TABLE 已经存在,那么...
insert into new_table
select * from old_table
/
如果您想根据 OLD_TABLE 中的记录创建 NEW_TABLE ...
create table new_table as
select * from old_table
/
如果目的是创建一个新的但为空的表,则使用 WHERE 子句,其条件永远不会为真:
create table new_table as
select * from old_table
where 1 = 2
/
请记住,CREATE TABLE ... AS SELECT 只创建一个与源表具有相同投影的表。新表没有原始表可能具有的任何约束、触发器或索引。这些仍然必须手动添加(如果需要)。
select into
在 pl/sql 中用于将变量设置为字段值。相反,使用
create table new_table as select * from old_table
select into
是 pl/sql 的一部分。它是一种用于编写存储过程的语言,与 sql 标准没有直接关系。是的,甲骨文做出了许多从未成为标准的东西=)
SELECT INTO
不是 标准 SQL。该标准仅定义 create table .. as select ..
利用:
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;
尝试使用以下语句而不是在 oracle 中使用 select into:
选择 * 从(从表 1 中选择 *)表 2;
不定期副业成功案例分享