Index: doc/src/sgml/plpgsql.sgml
===================================================================
RCS file: /projects/cvsroot/pgsql/doc/src/sgml/plpgsql.sgml,v
retrieving revision 1.156
diff -c -6 -r1.156 plpgsql.sgml
*** doc/src/sgml/plpgsql.sgml 29 Jul 2010 19:34:40 -0000 1.156
--- doc/src/sgml/plpgsql.sgml 5 Aug 2010 17:34:54 -0000
***************
*** 2332,2382 ****
linkend="errcodes-table"> for a list of possible error
codes). The SQLERRM variable contains the
error message associated with the exception. These variables are
undefined outside exception handlers.
-
- Exceptions with UPDATE>/INSERT>
-
-
- This example uses exception handling to perform either
- UPDATE> or INSERT>, as appropriate:
-
-
- CREATE TABLE db (a INT PRIMARY KEY, b TEXT);
-
- CREATE FUNCTION merge_db(key INT, data TEXT) RETURNS VOID AS
- $$
- BEGIN
- LOOP
- -- first try to update the key
- UPDATE db SET b = data WHERE a = key;
- IF found THEN
- RETURN;
- END IF;
- -- not there, so try to insert the key
- -- if someone else inserts the same key concurrently,
- -- we could get a unique-key failure
- BEGIN
- INSERT INTO db(a,b) VALUES (key, data);
- RETURN;
- EXCEPTION WHEN unique_violation THEN
- -- do nothing, and loop to try the UPDATE again
- END;
- END LOOP;
- END;
- $$
- LANGUAGE plpgsql;
-
- SELECT merge_db(1, 'david');
- SELECT merge_db(1, 'dennis');
-
-
-
-
Cursors
--- 2332,2343 ----