What is the best way to create a temporary table, if it does not already exist, and add the selected rows to it?
相关问题
- Django distinct is not working
- PostgreSQL: left outer join syntax
- Connecting Python to a Heroku PostgreSQL DB?
- PostgreSQL - Deleting data that are older than an
- Does PLV8 support making http calls to other serve
相关文章
- postgresql 关于使用between and 中是字符串的问题
- postgresql 月份差计算问题
- Using boolean expression in order by clause
- Table valued Parameter Equivalent in Postgresql
- in redshift postgresql can I skip columns with the
- Oracle equivalent of PostgreSQL INSERT…RETURNING *
- PostgreSQL field data type for IPv4 addresses
- Using prepared statement in stored function
CREATE TABLE AS
is the simplest and fastest way:
Not sure whether table already exists
CREATE TABLE IF NOT EXISTS ...
was introduced in version Postgres 9.1.For older versions, use the function provided in this related answer:
PostgreSQL create table if not exists
Then:
Chances are, something is going wrong in your code if the temp table already exists. Make sure you don't duplicate data in the table or something. Or consider the following paragraph ...
Unique names
Temporary tables are only visible within your current session (don't confuse with transaction!). So the table name cannot conflict with other sessions.
If you need unique names within your session, you could use dynamic SQL and utilize a
SEQUENCE
:Create once:
You could use a
DO
statement (or a plpgsql function):lastval()
andcurrval(regclass)
are instrumental in this case to get the name actually used.