Re: [SQL] Does PostgreSQL have an UPDATE Function like UNIFY? - Mailing list pgsql-sql

From Tom Lane
Subject Re: [SQL] Does PostgreSQL have an UPDATE Function like UNIFY?
Date
Msg-id 18074.927989679@sss.pgh.pa.us
Whole thread Raw
In response to Does PostgreSQL have an UPDATE Function like UNIFY?  ("John J. Boris, Sr." <john.boris@onlinesvc.com>)
List pgsql-sql
"John J. Boris, Sr." <john.boris@onlinesvc.com> writes:
> I was wondering if PostgreSQL had a function that allowed you to update a
> table from a delimited file that would update matching records and insert
> new records. Under Unify you could do an update with a switch (I think it
> was -n) that would take input from a pipe delimited file (in the order the
> fields of the table) and since the table had which field was unique it
> would key on that and if it didn't find the key it would add the
> record. 

Not directly, but I think you could solve it with a temporary table and
two or three SQL statements:
CREATE TABLE temp (just like the master)COPY temp FROM delimited fileUPDATE master SET field1 = temp.field1, etc
WHEREmaster.key = temp.keyINSERT INTO master SELECT * FROM temp    WHERE NOT EXISTS (SELECT * FROM master WHERE key =
temp.key)

There's probably a cleaner/more efficient way to do the last step...
but as long as you have an index on the key field it shouldn't be
too bad.
        regards, tom lane


pgsql-sql by date:

Previous
From: "D'Arcy" "J.M." Cain
Date:
Subject: Re: [SQL] Does PostgreSQL have an UPDATE Function like UNIFY?
Next
From: Herouth Maoz
Date:
Subject: Re: [SQL] 2 Table Select