|
1 | | -<!-- $PostgreSQL: pgsql/doc/src/sgml/plpgsql.sgml,v 1.95 2006/06/12 16:45:30 momjian Exp $ --> |
| 1 | +<!-- $PostgreSQL: pgsql/doc/src/sgml/plpgsql.sgml,v 1.96 2006/06/15 18:02:22 momjian Exp $ --> |
2 | 2 |
|
3 | 3 | <chapter id="plpgsql"> |
4 | 4 | <title><application>PL/pgSQL</application> - <acronym>SQL</acronym> Procedural Language</title> |
@@ -1081,7 +1081,7 @@ tax := subtotal * 0.06; |
1081 | 1081 | variable, or list of scalar variables. This is done by: |
1082 | 1082 |
|
1083 | 1083 | <synopsis> |
1084 | | -SELECT INTO <replaceable>target</replaceable> <replaceable>select_expressions</replaceable> FROM ...; |
| 1084 | +SELECT INTO <optional>STRICT</optional> <replaceable>target</replaceable> <replaceable>select_expressions</replaceable> FROM ...; |
1085 | 1085 | </synopsis> |
1086 | 1086 |
|
1087 | 1087 | where <replaceable>target</replaceable> can be a record variable, a row |
@@ -1122,47 +1122,43 @@ SELECT INTO <replaceable>target</replaceable> <replaceable>select_expressions</r |
1122 | 1122 | </para> |
1123 | 1123 |
|
1124 | 1124 | <para> |
1125 | | - If the query returns zero rows, null values are assigned to the |
1126 | | - target(s). If the query returns multiple rows, the first |
1127 | | - row is assigned to the target(s) and the rest are discarded. |
1128 | | - (Note that <quote>the first row</> is not well-defined unless you've |
1129 | | - used <literal>ORDER BY</>.) |
1130 | | - </para> |
1131 | | - |
1132 | | - <para> |
1133 | | - You can check the special <literal>FOUND</literal> variable (see |
1134 | | - <xref linkend="plpgsql-statements-diagnostics">) after a |
1135 | | - <command>SELECT INTO</command> statement to determine whether the |
1136 | | - assignment was successful, that is, at least one row was was returned by |
1137 | | - the query. For example: |
| 1125 | + If <literal>STRICT</literal> is not specified then |
| 1126 | + <replaceable>target</replaceable> will be set to the first row |
| 1127 | + returned by the query, or if the query returned no rows, |
| 1128 | + null values are assigned. (Note that <quote>the first row</> is not |
| 1129 | + well-defined unless you've used <literal>ORDER BY</>.) |
| 1130 | + You can check the special <literal>FOUND</literal> variable to |
| 1131 | + determine if any rows were found: |
1138 | 1132 |
|
1139 | 1133 | <programlisting> |
1140 | | -SELECT INTO myrec * FROM emp WHERE empname = myname; |
| 1134 | +SELECT INTOSTRICTmyrec * FROM emp WHERE empname = myname; |
1141 | 1135 | IF NOT FOUND THEN |
1142 | 1136 | RAISE EXCEPTION 'employee % not found', myname; |
1143 | 1137 | END IF; |
1144 | 1138 | </programlisting> |
1145 | | - </para> |
1146 | 1139 |
|
1147 | 1140 | <para> |
1148 | | - To test for whether a record/row result is null, you can use the |
1149 | | - <literal>IS NULL</literal> conditional. There is, however, no |
1150 | | - way to tell whether any additional rows might have been |
1151 | | - discarded. Here is an example that handles the case where no |
1152 | | - rows have been returned: |
1153 | | -<programlisting> |
1154 | | -DECLARE |
1155 | | - users_rec RECORD; |
1156 | | -BEGIN |
1157 | | - SELECT INTO users_rec * FROM users WHERE user_id=3; |
| 1141 | + If the <literal>STRICT</literal> option is specified, a query must |
| 1142 | + return exactly one row or a run-time error will be thrown, either |
| 1143 | + <literal>NO_DATA_FOUND</> (no rows) or <literal>TOO_MANY_ROWS</> |
| 1144 | + (more than one row). You can must use exception blocks to determine |
| 1145 | + the number of rows generated by the query: |
1158 | 1146 |
|
1159 | | - IF users_rec.homepage IS NULL THEN |
1160 | | - -- user entered no homepage, return "http://" |
1161 | | - RETURN 'http://'; |
1162 | | - END IF; |
| 1147 | +<programlisting> |
| 1148 | +BEGIN; |
| 1149 | + SELECT INTO STRICT myrec * FROM emp WHERE empname = myname; |
| 1150 | + EXCEPTION |
| 1151 | + WHEN NO_DATA_FOUND THEN |
| 1152 | + RAISE EXCEPTION 'employee % not found', myname; |
| 1153 | + WHEN TOO_MANY_ROWS THEN |
| 1154 | + RAISE EXCEPTION 'employee % not unique', myname; |
1163 | 1155 | END; |
1164 | 1156 | </programlisting> |
| 1157 | + Only <command>SELECT INTO STRICT</command> allows you to check if more |
| 1158 | + than one row was retrieved. <command>SELECT INTO STRICT</command> |
| 1159 | + matches Oracle's PL/SQL <command>SELECT INTO</command> behavior. |
1165 | 1160 | </para> |
| 1161 | + |
1166 | 1162 | </sect2> |
1167 | 1163 |
|
1168 | 1164 | <sect2 id="plpgsql-statements-perform"> |
@@ -1424,8 +1420,8 @@ GET DIAGNOSTICS integer_var = ROW_COUNT; |
1424 | 1420 | <itemizedlist> |
1425 | 1421 | <listitem> |
1426 | 1422 | <para> |
1427 | | - A <command>SELECT INTO</command> statement sets |
1428 | | - <literal>FOUND</literal> true ifit returnsa row, false if no |
| 1423 | + A <command>SELECT INTO</command> statement sets |
| 1424 | + <literal>FOUND</literal> true if a row is assigned, false if no |
1429 | 1425 | row is returned. |
1430 | 1426 | </para> |
1431 | 1427 | </listitem> |
|