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

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';

Related

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 SQL query result insert in temp table? [duplicate]

This question already has answers here:
How to save select query results within temporary table?
(3 answers)
Closed 4 years ago.
I have a SQL query (SQL Server) and it generate reports, I want to store that exact report in temp table so I can play with it later. Now question is do I need to create temp table first and then store SQL query result into it, or is there any way to dynamically create table and store query result?
Look at SELECT INTO. This will create a new table for you, which can be temporary if you want by prefixing the table name with a pound sign (#).
For example, you can do:
SELECT *
INTO #YourTempTable
FROM YourReportQuery
You can use select ... into ... to create and populate a temp table and then query the temp table to return the result.
select *
into #TempTable
from YourTable
select *
from #TempTable
In MySQL:
create table temp as select * from original_table
Try:
exec('drop table #tab') -- you can add condition 'if table exists'
exec('select * into #tab from tab')
Suppose your existing reporting query is
Select EmployeeId,EmployeeName
from Employee
Where EmployeeId>101 order by EmployeeName
and you have to save this data into temparory table then you query goes to
Select EmployeeId,EmployeeName
into #MyTempTable
from Employee
Where EmployeeId>101 order by EmployeeName

Creating new table with SELECT INTO in SQL [duplicate]

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.

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.

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;