Advertisement
Guest User

Untitled

a guest
Jul 24th, 2014
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. CREATE OR REPLACE FUNCTION add_entry(_user_name text)
  2. RETURNS bigint AS
  3. $BODY$
  4. DECLARE
  5. user_name text := '#' || $1;
  6. user_id bigint;
  7. BEGIN
  8.  
  9. -- get the ID from the sequence
  10. SELECT nextval('my_sequence') INTO user_id;
  11.  
  12. -- insert the name (and some other data not shown here) 5x
  13. FOR item IN 1..5
  14. LOOP
  15. INSERT INTO mytable
  16. (id,index,username)
  17. VALUES (user_id,item,user_name);
  18. END LOOP;
  19.  
  20. -- send notify that an insertion took place
  21. notify my_notify;
  22.  
  23. RETURN id;
  24. END;
  25. $BODY$
  26. LANGUAGE plpgsql VOLATILE
  27. COST 100;
  28.  
  29. select * from mytable where id = (select add_entry('MyTestUser'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement