|
1 | 1 | <!-- |
2 | | -$PostgreSQL: pgsql/doc/src/sgml/plpgsql.sgml,v 1.45 2004/08/08 22:40:46 tgl Exp $ |
| 2 | +$PostgreSQL: pgsql/doc/src/sgml/plpgsql.sgml,v 1.46 2004/08/16 17:52:06 tgl Exp $ |
3 | 3 | --> |
4 | 4 |
|
5 | 5 | <chapter id="plpgsql"> |
@@ -1098,6 +1098,52 @@ PERFORM create_mv('cs_session_page_requests_mv', my_query); |
1098 | 1098 | </para> |
1099 | 1099 | </sect2> |
1100 | 1100 |
|
| 1101 | + <sect2 id="plpgsql-statements-null"> |
| 1102 | + <title>Doing Nothing At All</title> |
| 1103 | + |
| 1104 | + <para> |
| 1105 | + Sometimes a placeholder statement that does nothing is useful. |
| 1106 | + For example, it can indicate that one arm of an if/then/else |
| 1107 | + chain is deliberately empty. For this purpose, use the |
| 1108 | + <command>NULL</command> statement: |
| 1109 | + |
| 1110 | +<synopsis> |
| 1111 | +NULL; |
| 1112 | +</synopsis> |
| 1113 | + </para> |
| 1114 | + |
| 1115 | + <para> |
| 1116 | + For example, the following two fragments of code are equivalent: |
| 1117 | +<programlisting> |
| 1118 | + BEGIN |
| 1119 | + y := x / 0; |
| 1120 | + EXCEPTION |
| 1121 | + WHEN division_by_zero THEN |
| 1122 | + NULL; -- ignore the error |
| 1123 | + END; |
| 1124 | +</programlisting> |
| 1125 | + |
| 1126 | +<programlisting> |
| 1127 | + BEGIN |
| 1128 | + y := x / 0; |
| 1129 | + EXCEPTION |
| 1130 | + WHEN division_by_zero THEN -- ignore the error |
| 1131 | + END; |
| 1132 | +</programlisting> |
| 1133 | + Which is preferable is a matter of taste. |
| 1134 | + </para> |
| 1135 | + |
| 1136 | + <note> |
| 1137 | + <para> |
| 1138 | + In Oracle's PL/SQL, empty statement lists are not allowed, and so |
| 1139 | + <command>NULL</> statements are <emphasis>required</> for situations |
| 1140 | + such as this. <application>PL/pgSQL</application> allows you to |
| 1141 | + just write nothing, instead. |
| 1142 | + </para> |
| 1143 | + </note> |
| 1144 | + |
| 1145 | + </sect2> |
| 1146 | + |
1101 | 1147 | <sect2 id="plpgsql-statements-executing-dyn"> |
1102 | 1148 | <title>Executing Dynamic Commands</title> |
1103 | 1149 |
|
@@ -1129,7 +1175,7 @@ EXECUTE <replaceable class="command">command-string</replaceable>; |
1129 | 1175 | <para> |
1130 | 1176 | When working with dynamic commands you will often have to handle escaping |
1131 | 1177 | of single quotes. The recommended method for quoting fixed text in your |
1132 | | - function body is dollar quoting. If you have legacy codewhich does |
| 1178 | + function body is dollar quoting. If you have legacy codethat does |
1133 | 1179 | not use dollar quoting, please refer to the |
1134 | 1180 | overview in <xref linkend="plpgsql-quote-tips">, which can save you |
1135 | 1181 | some effort when translating said code to a more reasonable scheme. |
@@ -1158,14 +1204,15 @@ EXECUTE <replaceable class="command">command-string</replaceable>; |
1158 | 1204 | </para> |
1159 | 1205 |
|
1160 | 1206 | <para> |
1161 | | - An example (this assumes that you are using dollar quoting, so the |
1162 | | - quote marks need not be doubled): |
| 1207 | + An example (this assumes that you are using dollar quoting for the |
| 1208 | +function as a whole, so thequote marks need not be doubled): |
1163 | 1209 | <programlisting> |
1164 | 1210 | EXECUTE 'UPDATE tbl SET ' |
1165 | 1211 | || quote_ident(colname) |
1166 | 1212 | || ' = ' |
1167 | 1213 | || quote_literal(newvalue) |
1168 | | - || ' WHERE ...'; |
| 1214 | + || ' WHERE key = ' |
| 1215 | + || quote_literal(keyvalue); |
1169 | 1216 | </programlisting> |
1170 | 1217 | </para> |
1171 | 1218 |
|
@@ -1193,7 +1240,8 @@ EXECUTE 'UPDATE tbl SET ' |
1193 | 1240 | || quote_ident(colname) |
1194 | 1241 | || ' = $$' |
1195 | 1242 | || newvalue |
1196 | | - || '$$ WHERE ...'; |
| 1243 | + || '$$ WHERE key = ' |
| 1244 | + || quote_literal(keyvalue); |
1197 | 1245 | </programlisting> |
1198 | 1246 | because it would break if the contents of <literal>newvalue</> |
1199 | 1247 | happened to contain <literal>$$</>. The same objection would |
|