Correct Usage of SELECT INTO Statement in Oracle and Common Misconceptions Analysis

Nov 18, 2025 · Programming · 10 views · 7.8

Keywords: Oracle Database | SELECT INTO | CREATE TABLE AS SELECT | INSERT INTO SELECT | ORA-00905 Error

Abstract: This article provides an in-depth exploration of the proper usage of SELECT INTO statements in Oracle Database, analyzes common ORA-00905 error causes,详细介绍介绍了CREATE TABLE AS SELECT and INSERT INTO SELECT alternative approaches with usage scenarios and considerations, and demonstrates through concrete code examples how to implement data table copying and creation operations in different situations.

Overview of SELECT INTO Statement in Oracle

In the Oracle database environment, the usage of SELECT INTO statements differs significantly from other database systems. Many developers encounter ORA-00905 errors when migrating from other databases to Oracle, typically due to misunderstandings of Oracle-specific syntax.

ORA-00905 Error Analysis

When developers attempt to execute SELECT * INTO new_table FROM old_table; statement, Oracle throws ORA-00905: missing keyword error. The root cause of this error lies in Oracle's SQL dialect implementation being different from other database systems. In Oracle, SELECT INTO statements are primarily used in PL/SQL programming environments for assigning query results to variables or records, not for creating new tables.

Correct Alternative Approaches

Creating New Table and Copying Data

If you need to create a new table based on the structure and data of an existing table, you should use CREATE TABLE AS SELECT statement:

CREATE TABLE new_table AS 
SELECT * FROM old_table;

This statement creates a new table named new_table with the same structure as old_table and copies all data from old_table to new_table.

Inserting Data into Existing Table

If the target table already exists and you need to insert data from the source table into the target table, you should use INSERT INTO SELECT statement:

INSERT INTO new_table 
SELECT * FROM old_table;

This usage requires that new_table must already exist and its column structure must be compatible with old_table.

Creating Empty Table Structure

If you only need to copy the table structure without data, you can add a WHERE condition that will never be true to the CREATE TABLE AS SELECT statement:

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

This technique leverages the filtering effect of WHERE condition. Since 1=2 is always false, no data will be copied, but an empty table with the same structure as the source table will be created.

Important Considerations

When using CREATE TABLE AS SELECT statement to create new tables, pay attention to the following points:

If these additional features are needed, you must manually add corresponding constraints, indexes, and triggers after creating the table.

SELECT INTO Usage in PL/SQL

In PL/SQL programming environment, SELECT INTO statement has its specific purpose, mainly used for assigning query results to variables or records. For example:

DECLARE
    v_employee_name employees.first_name%TYPE;
    v_salary employees.salary%TYPE;
BEGIN
    SELECT first_name, salary INTO v_employee_name, v_salary
    FROM employees 
    WHERE employee_id = 100;
    
    DBMS_OUTPUT.PUT_LINE('Employee: ' || v_employee_name || ', Salary: ' || v_salary);
END;

This usage requires that the query must return exactly one row of data, otherwise TOO_MANY_ROWS or NO_DATA_FOUND exceptions will be thrown.

Bulk Data Processing

For situations requiring processing multiple rows of data, you can use BULK COLLECT INTO statement:

DECLARE
    TYPE name_array IS TABLE OF employees.first_name%TYPE;
    v_names name_array;
BEGIN
    SELECT first_name BULK COLLECT INTO v_names
    FROM employees;
    
    FOR i IN 1..v_names.COUNT LOOP
        DBMS_OUTPUT.PUT_LINE('Name: ' || v_names(i));
    END LOOP;
END;

This method loads the entire result set into collection variables at once, improving data processing efficiency.

Best Practice Recommendations

When using Oracle's data copying and creation features, it's recommended to follow these best practices:

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.