Creating new table with SELECT INTO in SQL [duplicate] - sql

This question already has answers here:
Closed 10 years ago.
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.

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.

Related

Teradata query using a large list of values in WHERE predicate

I'm attempting to query Teradata in SQL Assistant, something along the lines of
select * from
addressTable
where accountNumber in
(
'AC001',
'AC098',
'AC711',
...
)
from a list of over 100,000 accountNumbers sent to me in a file.
What I have tried
StackOverflow past questions - all seem to say that you need to insert into a temp table using a select clause from a table within the same database
Teradata doco on the link below. When I go to create table globdb.gt1, I get error 'CREATE TABLE Failed 3802: Database 'globdb' does not exist.
https://docs.teradata.com/r/Teradata-Database-SQL-Fundamentals/June-2017/Database-Objects/Tables/Global-Temporary-Tables

Check a Table Available in a Particular Database in Sql Server [duplicate]

This question already has answers here:
Check if table exists and if it doesn't exist, create it in SQL Server 2008
(8 answers)
Closed 2 years ago.
I have two Databases db1 and db2. In those two databases I have a table name "Asset_table" with same columns and same table structure. But In Some client's db2 does not have the "Asset_table".
First I need to check "Asset_table" is available in the client database and if it is available then insert data from db1 to that client DB table.
Here is my query.
IF EXISTS (--here I need to check if the table is available in the db2---)
BEGIN
INSERT INTO db2.Asset_table( asset_id, name,qty,description)
SELECT asset_id, name,qty,description
FROM db1.Asset_table
END
Can any one sugest me an script for this?
Is this what you need?
IF NOT EXISTS(Select 1 from db2.INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA='dbo' AND TABLE_NAME='Asset_table')
BEGIN
CREATE TABLE db2.dbo.Asset_table(asset_id int, name nvarchar(50),qty int,description nvarchar(150));
END
INSERT INTO db2.dbo.Asset_table( asset_id, name,qty,description)
SELECT asset_id, name,qty,description
FROM db1.dbo.Asset_table;

Is there a way to create a temporary table in SQL that deletes right after the query finishes? [duplicate]

This question already has answers here:
Creating temporary tables in SQL
(2 answers)
Closed 6 years ago.
I have a complicated query I'm working on. It involves several tables.
It would be very helpful for me to create a new table and then simply query from that. However, this is a shared database and I don't want to make a new table, especially when i don't plan on using that table specifically. (I just want it as a stepping stone in my query)
Is it possible to create a table just for 1 query that deletes right when the query is done? (i.e a temporary table)
Sure. Use CREATE TEMPORARY TABLE:
=> CREATE TEMPORARY TABLE secret_table(id BIGSERIAL, name text);
=> INSERT INTO secret_table(name) VALUES ('Good day');
INSERT 0 1
=> INSERT INTO secret_table(name) VALUES ('Good night');
INSERT 0 1
=> SELECT * FROM secret_table;
id | name
----+------------
1 | Good day
2 | Good night
(2 rows)
But upon reconnection:
psql (9.5.4)
Type "help" for help.
=> SELECT * FROM secret_table;
ERROR: relation "secret_table" does not exist
LINE 1: SELECT * FROM secret_table;
You could use temporary tables which drops itself at the end of session in which they were created (not after the query finishes, as you've said). Though, you could always drop it manually at the end of your operation.
If you'd like to create such table as a result from a query then this is the sample to be expanded to your needs:
CREATE TEMP TABLE tmp_table_name AS ( SELECT 1 AS col1 );
But I'm thinking you may be looking for a CTE instead of a table since you're saying that you're planning to use it only once. Consider this:
WITH tmp_table AS ( SELECT 1 AS col1 )
SELECT *
FROM tmp_table
...
You can also do dinamically The result of a query is also a Table
select * from (select col1, col2, col3
from my_complex_table
... ) t1
use keyword temporary, the temporary table is only visible in your current connection and drop after you disconnect your connection.
The other way would create a table and drop the table by yourself when you don't need it

How can I create a SQL table from another table without copying any values from the old table [duplicate]

This question already has answers here:
How can I create a copy of an Oracle table without copying the data?
(17 answers)
Closed 9 years ago.
How to create table with existing table structure without iterate row by row like this in Oracle? Thanks in Advance.
CREATE TABLE new_table
AS (SELECT *
FROM old_table WHERE 1=2);
If you are worried about iterating through the table:
CREATE TABLE new_table
AS (SELECT *
FROM (select * old_table where rownum = 1) t
WHERE 1=2
);
I have already read about this.. Hope it gives a Detailed explanation to you..
What we ended up doing in this clients case was to replace the “WHERE 1=2” with a clause that equated the primary key of the table with an impossible value for that key, in this case the ID was being passed in as a GUID (a hexadecimal value) so we use a “WHERE KEY=HEX(00)” and got a low cost unique index lookup instead of a costly full table scan.
http://www.dba-oracle.com/oracle_tips_ault_where_1_equals_2_parallel_.htm
Thanks to Burleson Consulting
I'm not sure on the exact Oracle syntax but in virtually any SQL if you open up the other table using a GUI tool there are options both to generate a create script statement for the table and to backup the table without data.
Either of those will do what you need.

How do you use SQL to see whether a table exists or not? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
SQL Server: Check if table exists
Oracle: If Table Exists
I'm trying to create a table and insert some values, but before I do I must make sure that the table doesn't already exist. How do you check for this?
You can try simply SELECTing from it and capturing the error, otherwise you will need to write DB specific SQL to query their respective metadata tables and look there.
Assuming SQL Server...Query sysobjects:
select * from sysobjects where xtype='U' and name ='tablename'
In MySQL, you can use the CREATE TABLE IF NOT EXISTS construct and run it before your INSERT query. This will create the table if it doesn't exist and will do nothing if the table is there.
CREATE TABLE IF NOT EXISTS myTable (
....
)
http://dev.mysql.com/doc/refman/5.1/en/create-table.html
http://docs.oracle.com/cd/E17952_01/refman-5.1-en/create-table.html
IF MySQL:
select count(*) from my_tables where table_name='table_1';
If count>0 then ...
This simple query gives you details about the particular table which are created by users in oracle. Try it.
select * from user_tables where table_name = 'tablename';