HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMinor

Create INSERT INTO from SELECT in PostgreSQL

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
postgresqlinsertcreateintoselectfrom

Problem

How to create INSERT INTO statements, using SELECT statement? I need it because I want to delete 10k of records from a table, but if someday some of that record need to be restored I want to do that easily. I don't want to backup the whole table, because I just need to restore some of deleted rows.

Solution

If you really needs the insert statement using a query:

select 'INSERT INTO table values ('||bla_integer||', '''||bla_string||''')'
   from thetable; 

   Where bla_string and bla_integer are example columns from thetable...

Code Snippets

select 'INSERT INTO table values ('||bla_integer||', '''||bla_string||''')'
   from thetable; 

   Where bla_string and bla_integer are example columns from thetable...

Context

StackExchange Database Administrators Q#135417, answer score: 7

Revisions (0)

No revisions yet.