Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select into temp table in PostgreSQL? [duplicate]

Tags:

sql

postgresql

How to create temporary table using select into in PostgreSQL. For example in SQL Select * into temp_tab from source_tab;

like image 466
Parthasarathi Avatar asked Apr 30 '15 10:04

Parthasarathi


People also ask

How do I copy a table into a temp table in SQL?

INSERT INTO SELECT statement reads data from one table and inserts it into an existing table. Such as, if we want to copy the Location table data into a temp table using the INSERT INTO SELECT statement, we have to specify the temporary table explicitly and then insert the data.

How do I insert stored procedure results into temp table in SQL?

Step 1: Add "into #temp" to the output query (e.g. "select [...] into #temp from [...]"). The easiest way is to edit the output query in the proc directly. if you can't change the stored proc, you can copy the contents into a new query window and modify the query there.

Can we create temporary table in PostgreSQL?

Syntax to create PostgreSQL Temporary tables In the syntax, Specify the TEMP or TEMPORARY keyword after the CREATE keyword. Specify the list of columns with datatype after the name of the temp table.


1 Answers

You can try to use Create Table As command like this:

CREATE TEMP TABLE mytable AS SELECT * from source_tab; 

From the docs:

This command is functionally similar to SELECT INTO, but it is preferred since it is less likely to be confused with other uses of the SELECT INTO syntax. Furthermore, CREATE TABLE AS offers a superset of the functionality offered by SELECT INTO.

The CREATE TABLE AS command allows the user to explicitly specify whether OIDs should be included. If the presence of OIDs is not explicitly specified, the default_with_oids configuration variable is used.

like image 182
Rahul Tripathi Avatar answered Sep 24 '22 17:09

Rahul Tripathi