Create table (structure and data) from existing table in oracle - sql

I want to create (and fill) a table similar to an existing table in Oracle (PL-SQL).
I used this code (extracted from this question):
Select * into NewTable from OldTable
but this error occurred:
ORA-00905: missing keyword
I think this code works only in SQL-Server. so how can I handle it in Oracle?

CREATE TABLE NewTable AS
SELECT * FROM OldTable

Related

CREATE TABLE ... LIKE not working

Need help trying to create a table using CREATE TABLE ... LIKE, code is below, error given is
Incorrect syntax near the keyword 'LIKE'
CREATE TABLE jobserve_reports LIKE Jobserve
I've tried putting the LIKE in quotations with no luck, tried making it a temporary table, using curly braces and nothing has worked. Am at my wits end.
In SQL Server, we can create using:
SELECT *
INTO newtable
FROM oldtable
WHERE 1 = 0;
If LIKE is not available in your db you could use create select
CREATE TABLE jobserve_reports AS
select * from Jobserve
or equivalent
eventuallly using
CREATE TABLE jobserve_reports AS
select * from Jobserve
where 1 = 2
for get no result
For Postgres, MS sql-server and Oracle use the AS keyword:
CREATE TABLE new_table
AS SELECT * FROM old_table;
In MySQL you can use the AS keyword as well, but it is optional:
CREATE TABLE new_table
AS SELECT * FROM old_table;
or
CREATE TABLE new_table
SELECT * FROM old_table;

Duplicating a table in dashDB

I want to create a duplicate table in dashDB (with or without the original data, it does not really matter, the table structure is what is important).
I tried:
SELECT * INTO new_table
FROM old_table;
but I get this error:
"new_table" is not valid in the context where it is used.. SQLCODE=-206, SQLSTATE=42703, DRIVER=3.69.56
I also tried:
CREATE TABLE new_table AS
(SELECT * FROM old_table);
but I get this error:
An unexpected token "END-OF-STATEMENT" was found following "AS (SELECT * FROM old_table". Expected tokens may include: "WITH DATA, WITH NO DATA".. SQLCODE=-104, SQLSTATE=42601, DRIVER=3.69.56
I found an answer here:
Create table (structure) from existing table
seems this did the trick
CREATE TABLE new_table LIKE old_table;
Just do this. This will work and create an exact copy of the table including data:
CREATE TABLE NEW_TAB AS (SELECT * FROM EXISTING_TAB) WITH DATA;

How to copy a table schema with sql based on existance of the table?

I want to duplicate a table schema, but only if the target table does not yet exist:
CREATE TABLE IF NOT EXISTS my_table_secondary as select * from my_table_primary
Problem: running this if the my_secondary_table exists results in:
ERROR: Syntaxerror at "as".
What could I do to make this sql statement work?
(postgres 9.4)
To duplicate the schema of a table, you need a completely different syntax.
create table if not exists my_table_secondary (
like my_table_primary including all
);
INCLUDING ALL is an abbreviated form of INCLUDING DEFAULTS INCLUDING
CONSTRAINTS INCLUDING INDEXES INCLUDING STORAGE INCLUDING COMMENTS.
It doesn't duplicate foreign key constraints. See CREATE TABLE. Search for "The LIKE clause".
Try this approach:
CREATE OR REPLACE VIEW my_view AS SELECT * FROM my_table_primary;
CREATE TABLE IF NOT EXISTS my_table_secondary LIKE my_view;
INSERT INTO my_table_secondary
SELECT * FROM my_view
WHERE NOT EXISTS ( SELECT * FROM my_table_secondary );
More on CREATE TABLE ... LIKE: http://www.postgresql.org/docs/9.1/static/sql-createtable.html

copy the tables after execution

I would like to create new table after executing that query
create table newTable as select * from oldTable
However, this does not appear to work. How do I get the new table after executing some queries?
The syntax in general is like:
CREATE TABLE new_table
AS (SELECT * FROM old_table);
For example:
CREATE TABLE suppliers
AS (SELECT id, address, city, state, zip
FROM companies
WHERE id > 1000);
Try removing the stars (*) and add the brackets.
Read here for more examples.
I am not sure what DBMS you are using or what errors you are getting, so I will try to answer for multiple systems.
If you are working with Oracle or PostgreSQL (there might be some other systems that this rule applies to), your syntax seems to be correct. Just make sure your new table doesn't exist yet - otherwise it's going to error out. In case if you are trying to insert into an existing table - which I don't think the case is, however - you can try something like -
INSERT INTO newTable SELECT * FROM oldTable
On the other hand, if you are working with T-SQL (SQL Server), you could SELECT INTO the new table. The new table will be created with the old table's schema.
You can read more about the INTO Clause at MSDN Library.
Your code should look like -
SELECT *
INTO newTable
FROM oldTable
And, specifying the column names and filters also works the similar way -
SELECT Column1, Column2, Column3, ...
INTO newTable
FROM oldTable
WHERE <Filter Condition>
Whatever the case is, you would get more help if you specify the details.
As You Said you want to copy the values into new table after execution
whether if you are running the stored procedure using cursor let the cursor shuld be closed then use query as follows
Select * into Table1 from Table2
if you want to copy selected colums go for
Select Coloumn1 ,column2,... into table1 from table 2 where ............

dynamically create table statement in sql

how to dynamically create a table with same columns as that of previous table. in sql
select * into new_table from table where 1 = 0
select * into new_table from table
Thats works in SQL2005
I believe that both of the above answers will work, but since you don't need the data and you just need the format, I would do the following:
select * into new_table from table
TRUNCATE new_table; -- I'm sure you know this, but just in case someone is new and doesn't, truncate leaves the table structure and removes all of the data.