Advertisement
oquidave

sql_select_insert

Aug 20th, 2011
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. BEGIN
  2. LOOP
  3. -- first check if this number is already present in list with id 1
  4. SELECT * from test_co.list_numbers where lists_id='1' and numbers_id= '2';
  5. IF found THEN
  6. RETURN;
  7. END IF;
  8. -- not there, so try to insert the phone_id and the list_id
  9. -- if someone else inserts the same key concurrently,
  10. -- we could get a unique-key failure
  11. BEGIN
  12. --if it's not present, then insert it
  13. INSERT INTO test_co.list_numbers (lists_id, numbers_id) VALUES (1, 2);
  14. RETURN;
  15. EXCEPTION WHEN unique_violation THEN
  16. -- do nothing, and loop to try the UPDATE again
  17. END;
  18. END LOOP;
  19. END;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement