Home >Database >Mysql Tutorial >How to Correctly Use 'SELECT INTO' in Oracle to Avoid ORA-00905 Errors?

How to Correctly Use 'SELECT INTO' in Oracle to Avoid ORA-00905 Errors?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-04 06:09:43790browse

How to Correctly Use

Troubleshooting "SELECT INTO" Error in Oracle

The "SELECT INTO" statement in Oracle, which inserts the results of a SELECT query into a new or existing table, can result in an ORA-00905 "missing keyword" error. This error message indicates a discrepancy between the Oracle implementation and the expected standard behavior.

Oracle Implementation:

Unlike the standard implementation, Oracle requires a different syntax for "SELECT INTO":

  • If the target table already exists:
    Use the "INSERT INTO ... SELECT" syntax:

    INSERT INTO new_table SELECT * FROM old_table;
  • If the target table doesn't exist and you want to create it:
    Use the "CREATE TABLE ... AS SELECT" syntax:

    CREATE TABLE new_table AS SELECT * FROM old_table;

Other Considerations:

If the target table exists and you want to create an empty duplicate, use the following syntax:

CREATE TABLE new_table AS SELECT * FROM old_table WHERE 1 = 2;

Remember that the "CREATE TABLE ... AS SELECT" method only creates a table with the same column projection as the source table. To add constraints, triggers, or indexes, you must manually create them separately.

The above is the detailed content of How to Correctly Use 'SELECT INTO' in Oracle to Avoid ORA-00905 Errors?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn