From a584c12426ae07d6d765c0c321ced5726e497044 Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Thu, 15 Jun 2006 18:02:22 +0000 Subject: Add STRICT to PL/pgSQL SELECT INTO, so exceptions are thrown if more or less than one row is returned by the SELECT, for Oracle PL/SQL compatibility. Improve SELECT INTO documentation. Matt Miller --- doc/src/sgml/plpgsql.sgml | 62 ++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 33 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/plpgsql.sgml b/doc/src/sgml/plpgsql.sgml index 60c7593362b..1f09b9e99c7 100644 --- a/doc/src/sgml/plpgsql.sgml +++ b/doc/src/sgml/plpgsql.sgml @@ -1,4 +1,4 @@ - + <application>PL/pgSQL</application> - <acronym>SQL</acronym> Procedural Language @@ -1081,7 +1081,7 @@ tax := subtotal * 0.06; variable, or list of scalar variables. This is done by: -SELECT INTO target select_expressions FROM ...; +SELECT INTO STRICT target select_expressions FROM ...; where target can be a record variable, a row @@ -1122,47 +1122,43 @@ SELECT INTO target select_expressions - If the query returns zero rows, null values are assigned to the - target(s). If the query returns multiple rows, the first - row is assigned to the target(s) and the rest are discarded. - (Note that the first row is not well-defined unless you've - used ORDER BY.) - - - - You can check the special FOUND variable (see - ) after a - SELECT INTO statement to determine whether the - assignment was successful, that is, at least one row was was returned by - the query. For example: + If STRICT is not specified then + target will be set to the first row + returned by the query, or if the query returned no rows, + null values are assigned. (Note that the first row is not + well-defined unless you've used ORDER BY.) + You can check the special FOUND variable to + determine if any rows were found: -SELECT INTO myrec * FROM emp WHERE empname = myname; +SELECT INTO STRICT myrec * FROM emp WHERE empname = myname; IF NOT FOUND THEN RAISE EXCEPTION 'employee % not found', myname; END IF; - - To test for whether a record/row result is null, you can use the - IS NULL conditional. There is, however, no - way to tell whether any additional rows might have been - discarded. Here is an example that handles the case where no - rows have been returned: - -DECLARE - users_rec RECORD; -BEGIN - SELECT INTO users_rec * FROM users WHERE user_id=3; + If the STRICT option is specified, a query must + return exactly one row or a run-time error will be thrown, either + NO_DATA_FOUND (no rows) or TOO_MANY_ROWS + (more than one row). You can must use exception blocks to determine + the number of rows generated by the query: - IF users_rec.homepage IS NULL THEN - -- user entered no homepage, return "http://" - RETURN 'http://'; - END IF; + +BEGIN; + SELECT INTO STRICT myrec * FROM emp WHERE empname = myname; + EXCEPTION + WHEN NO_DATA_FOUND THEN + RAISE EXCEPTION 'employee % not found', myname; + WHEN TOO_MANY_ROWS THEN + RAISE EXCEPTION 'employee % not unique', myname; END; + Only SELECT INTO STRICT allows you to check if more + than one row was retrieved. SELECT INTO STRICT + matches Oracle's PL/SQL SELECT INTO behavior. + @@ -1424,8 +1420,8 @@ GET DIAGNOSTICS integer_var = ROW_COUNT; - A SELECT INTO statement sets - FOUND true if it returns a row, false if no + A SELECT INTO statement sets + FOUND true if a row is assigned, false if no row is returned. -- cgit v1.2.3