Jump to content


This topic is now archived and is closed to further replies.


INSERT problem

Recommended Posts

I have the following SQL I am trying to run from pgadmin III

INSERT INTO master.employment(employer_id, employee_id, status_id,imported_by)
SELECT  (SELECT id FROM businesses.employers WHERE sport_id=1 AND employer=imports.menssoccer.client) as employer_id
,(SELECT id FROM users.employees WHERE tax_id=imports.menssoccer.ssn AND SSN Is Not Null) as employee_id
FROM imports.menssoccer

The problem is that sometimes it violate a Unique Constraint on the item. Is there a way I can just insert the rows that dont't exist? Or can I ignore the errors somehow and just go to the next row. Thanks

Its not as easy a regular old IF NOT EXISTS...

Share this post

Link to post
Share on other sites
Which unique constraint is being violated?  Can you paste the error message?

If you're getting "duplicate" rows from a query, you can use 'select distinct on ( ... )' to make them unique according to certain conditions.  The particular row you get will be random unless you order your results.

By "duplicate" I mean that the rows are not identical, but they still violate some kind of uniqueness constraint that you want to enforce.

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.