Jump to content


Photo

INSERT problem


  • Please log in to reply
1 reply to this topic

#1 hc

hc
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 18 September 2006 - 10:23 PM

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
,1
,'test'
FROM imports.menssoccer
WHERE SSN Is Not Null

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...

#2 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 19 September 2006 - 12:56 AM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users