0

Let's say I have a table item and an other one versionned_item. From time to time, I want to copy data from item to versionned_item. What I did is

   WITH
     subset AS (SELECT ...)
     INSERT INTO versionned_item SELECT ... FROM subset

But in the documentation I came across the SELECT INTO statement that seems to do the exact same thing so I am wondering if there are differences / good practices about those 2 kind of inserts.

  • 2
    select into is either used inside PL/pgSQL to store the result of a select in a variable or as a non-standard replacement for create table .. as select ... – a_horse_with_no_name Aug 27 '14 at 8:34
  • A better read of the documentation showed anonymous horse made the best answer. Thx – greg Aug 27 '14 at 8:38
1

SELECT INTO is usually used to select specific set of data into a table, esp., during scenarios when the data in the table is the priority and not the constraints. It automatically creates a table if there is no such table already. But, INSERT INTO is used when you already have a table that has specific defined constraints and need to add data from a different table.

  • True, trying to select into an existing table pops an error out. – greg Aug 27 '14 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.