Advertisement
Guest User

Untitled

a guest
Mar 28th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. CREATE TABLE mytable (id SERIAL, name VARCHAR(10) PRIMARY KEY)
  2.  
  3. INSERT INTO mytable (name) VALUES ('Jonas') RETURNING id
  4.  
  5. CREATE OR REPLACE FUNCTION upsert_tableName(arg1 type, arg2 type) RETURNS VOID AS $$
  6. DECLARE
  7. BEGIN
  8. UPDATE tableName SET col1 = value WHERE colX = arg1 and colY = arg2;
  9. IF NOT FOUND THEN
  10. INSERT INTO tableName values (value, arg1, arg2);
  11. END IF;
  12. END;
  13. $$ LANGUAGE 'plpgsql';
  14.  
  15. BEGIN
  16. INSERT INTO db_table (tbl_column) VALUES (v_tbl_column);
  17. EXCEPTION WHEN unique_violation THEN
  18. -- Ignore duplicate inserts.
  19. END;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement