Temporary tables and AS statement usage - sql

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

Related

How to create a temporary table without referring to permanent table i.e. without using select statement

Is there any way to create our own temporary table or using 'With' clause to create a table with our own columns and values without using any select statement from another permanent table in a query. That temporary table I want to refer in my main query.
Please help me with this.
Thanks.
You can certainly use a WITH however depending on the data you might have to use a lot of UNION ALL statements unless your requirement can allow to use some kind of recursive query. The below is for SQL-Server.
WITH temp_table(id, name) AS
(
SELECT 1, 'abc'
UNION ALL
SELECT 2, 'cde'
)
SELECT * FROM temp_table
You can read this for more details - https://learn.microsoft.com/en-us/sql/t-sql/queries/with-common-table-expression-transact-sql?view=sql-server-ver15

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

Is there an automated way to create a temp table in SQL Server

I have a rather complex SELECT statement in a stored procedure that I am updating to insert the rows from the select into a temp table. To define the temp table, I need to know the data type of each every item selected.
Is there a easy way (a script maybe) that I can use to determine the data types and the temp table structure instead of going to each table's definition in the select to find out what it is?
PS: I can't use a Common table expression as I need to use this temp table several times within the proc
SELECT
blah
INTO
#temp
FROM
wibble
blah and wibble are not secret syntax. Please replace these with your own SQL :)
SELECT * INTO #temp FROM TABLE1
All columns in TABLE 1 gets into your temp table now

"select * into table" Will it work for inserting data into existing table

I am trying to insert data from one of my existing table into another existing table.
Is it possible to insert data into any existing table using select * into query.
I think it can be done using union but in that case i need to record all data of my existing table into temporary table, then drop that table and finally than apply union to insert all records into same table
eg.
select * into #tblExisting from tblExisting
drop table tblExisting
select * into tblExisting from #tblExisting union tblActualData
Here tblExisting is the table where I actually want to store all data
tblActualData is the table from where data is to be appended to tblExisting.
Is it right method.
Do we have some other alternative ?
You should try
INSERT INTO ExistingTable (Columns,..)
SELECT Columns,...
FROM OtherTable
Have a look at INSERT
and SQL SERVER – Insert Data From One Table to Another Table – INSERT INTO SELECT – SELECT INTO TABLE
No, you cannot use SELECT INTO to insert data into an existing table.
The documentation makes this very clear:
SELECT…INTO creates a new table in the default filegroup and inserts the resulting rows from the query into it.
You generally want to avoid using SELECT INTO in production because it gives you very little control over how the table is created, and can lead to all sorts of nasty locking and other performance problems. You should create schemas explicitly and use INSERT - even for temporary tables.
#Ryan Chase
Can you do this by selecting all columns using *?
Yes!
INSERT INTO yourtable2
SELECT * FROM yourtable1
Update from CTE? http://www.sqlservercentral.com/Forums/Topic629743-338-1.aspx

In MySQL, how to copy the content of one table to another table within the same database?

I am new to MySQL. I would like to copy the content of one table to another table within the same database. Basically, I would like to insert to a table from another table. Is there easy way of doing this?
If the tables have the same structure:
INSERT INTO TARGET_TABLE SELECT * FROM SOURCE_TABLE;
If the tables have different structures:
INSERT INTO TARGET_TABLE (`col1`,`col2`) SELECT `col1`,`col2` FROM SOURCE_TABLE;
You can also add conditions:
INSERT INTO TARGET_TABLE (`col1_`,`col2_`) SELECT `col1`,`col2` FROM SOURCE_TABLE WHERE `foo`=1
If the table doesn't exist, you can create one with the same schema like so:
CREATE TABLE table2 LIKE table1;
Then, to copy the data over:
INSERT INTO table2 SELECT * FROM table1
If table1 is large and you don't want to lock it for the duration of the copy process, you can do a dump-and-load instead:
CREATE TABLE table2 LIKE table1;
SELECT * INTO OUTFILE '/tmp/table1.txt' FROM table1;
LOAD DATA INFILE '/tmp/table1.txt' INTO TABLE table2;
This worked for me,
CREATE TABLE newtable LIKE oldtable;
Replicates newtable with old table
INSERT newtable SELECT * FROM oldtable;
Copies all the row data to new table.
If you want to create and copy the content in a single shot, just use the SELECT:
CREATE TABLE new_tbl SELECT * FROM orig_tbl;
This worked for me. You can make the SELECT statement more complex, with WHERE and LIMIT clauses.
First duplicate your large table (without the data), run the following query, and then truncate the larger table.
INSERT INTO table_small (SELECT * FROM table_large WHERE column = 'value' LIMIT 100)
Super simple. :-)
CREATE TABLE target_table SELECT * FROM source_table;
It just create a new table with same structure as of source table and also copy all rows from source_table into target_table.
CREATE TABLE target_table SELECT * FROM source_table WHERE condition;
If you need some rows to be copied into target_table, then apply a condition inside where clause
Try this. Works well in my Oracle 10g,
CREATE TABLE new_table
AS (SELECT * FROM old_table);