CREATE TABLE AS SELECT - impala

I am trying a simple CREATE TABLE AS SELECT in Impala 2.3.0 and it doesn't seem to work.
CREATE TABLE clone_of_t1 AS SELECT * FROM t1;
Query: create TABLE clone_of_t1 AS SELECT * FROM t1 ERROR:
AnalysisException: testdb CAUSED BY: NoSuchObjectException: testdb
Are there any special options required for this to work?
Thanks.

It looks like the database testdb doesn't exist. You could look your JDBC url to check if the database name is correct, it should be similar to this:
jdbc:impala://{host}:{port}/{database}

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;

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

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

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

How can I create a table as a select from another database in Oracle?

Is it possible to create a table (in my dev db) using a SELECT from a different database?
I want something like:
create tmp_table as select * from prod_db.prod_schema.table
Is there syntax to do this, or do I need to create a database link first?
You have to create a datalink first.
Oracle cannot query other databases unless a DB link is created. If a DB link exists, as you remarked, you have to do :
create tmp_table as select * from prod_schema.table#prod_db
#Steve is correct that there has to be a DB Link, but the syntax is:
create tmp_table as select * from table#dblink
Don't forget to create your indexes. You can get this for all the tables in your schema with a query like this:
SELECT DBMS_METADATA.GET_DDL('INDEX',u.index_name)
FROM USER_INDEXES u;
CREATE TABLE table_name
AS SELECT * FROM schema_name.table_name;