Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating new table with SELECT INTO in SQL [duplicate]

Possible Duplicate:
SELECT INTO using Oracle

I have came across SQL SELECT INTO statement for creating new table and also dumping old table records into new table in single SQL statement as

  SELECT * INTO NEW_TABLE FROM OLD_TABLE; 

But when i am trying the above query it is giving error as ORA-00905: missing keyword

Is it possible to do that in ORACLE ?

Thanks.

like image 737
Aspirant Avatar asked Aug 09 '12 21:08

Aspirant


People also ask

How can we create a duplicate table using SQL query?

Use Transact-SQLRight-click the table you wish to duplicate, point to Script Table as, then point to CREATE to, and then select New Query Editor Window. Change the name of the table. Remove any columns that are not needed in the new table. Select Execute to create the new table.

Does SELECT into create a new table?

The SELECT INTO statement creates a new table and inserts rows from the query into it. If you want to copy the partial data from the source table, you use the WHERE clause to specify which rows to copy.

How will you create a table based on SELECT statement in SQL?

If you would like to create a new table, the first step is to use the CREATE TABLE clause and the name of the new table (in our example: gamer ). Then, use the AS keyword and provide a SELECT statement that selects data for the new table.

How do you create and copy data from one table to another in SQL?

The SQL INSERT INTO SELECT Statement The INSERT INTO SELECT statement copies data from one table and inserts it into another table. The INSERT INTO SELECT statement requires that the data types in source and target tables match. Note: The existing records in the target table are unaffected.


1 Answers

The syntax for creating a new table is

CREATE TABLE new_table AS SELECT *   FROM old_table 

This will create a new table named new_table with whatever columns are in old_table and copy the data over. It will not replicate the constraints on the table, it won't replicate the storage attributes, and it won't replicate any triggers defined on the table.

SELECT INTO is used in PL/SQL when you want to fetch data from a table into a local variable in your PL/SQL block.

like image 78
Justin Cave Avatar answered Sep 29 '22 10:09

Justin Cave