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
Related
I am new to HiveQL and I need to create a temp table from the results of following query:
SELECT * FROM `database`.`table` LIMIT 0,100;
....Then run query against this temp table. How could I accomplish this and what is the best practice?
So i guess the SQL Anywhere version would be something like:
create table #temp (foo int)
insert into #temp (foo)
select top (100) 1
from dbo.table t
select count(*) from #temp as c
drop table #temp
Any help would be greatly appreciated. Thanks in advance!
In hive you can create similar functionality using CREATE TEMPORARY TABLE . Table can me managed or external and will stay as long as your hive session is active. Also, pls note, if you are running it as part of batch process, this may not be a good idea.
CREATE TEMPORARY TABLE etl.tmp1 as select * from tab limit 10;
select count(*) from etl.tmp1 ;
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
If I have a query, say:
SELECT *
FROM ( SELECT fields
FROM tables
WHERE condition
)
AS TEMP_TABLE
are the results of the above query saved in a temporary table called TEMP_TABLE so as I can perform another query on it later? Will the query below be executed successfully when using DB2?
SELECT fields
FROM TEMP_TABLE
WHERE condition
The answer is NO, it's just an alias for a subquery.
If you want to use it later, you have to explicitly create it.
you can create temporary table in following way.
CREATE TEMPORARY TABLE temp_table AS (
SELECT fields
FROM tables
WHERE condition
);
then you can retrieve data from the temp table as below.
SELECT * FROM temp_table
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';
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.