~ubuntu-branches/ubuntu/oneiric/postgresql-9.1/oneiric-security

« back to all changes in this revision

Viewing changes to doc/src/sgml/html/plpgsql-statements.html

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-11 10:41:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110511104153-psbh2o58553fv1m0
Tags: upstream-9.1~beta1
ImportĀ upstreamĀ versionĀ 9.1~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 
2
<HTML
 
3
><HEAD
 
4
><TITLE
 
5
>Basic Statements</TITLE
 
6
><META
 
7
NAME="GENERATOR"
 
8
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
 
9
REV="MADE"
 
10
HREF="mailto:pgsql-docs@postgresql.org"><LINK
 
11
REL="HOME"
 
12
TITLE="PostgreSQL 9.1beta1 Documentation"
 
13
HREF="index.html"><LINK
 
14
REL="UP"
 
15
TITLE="PL/pgSQL - SQL Procedural Language"
 
16
HREF="plpgsql.html"><LINK
 
17
REL="PREVIOUS"
 
18
TITLE="Expressions"
 
19
HREF="plpgsql-expressions.html"><LINK
 
20
REL="NEXT"
 
21
TITLE="Control Structures"
 
22
HREF="plpgsql-control-structures.html"><LINK
 
23
REL="STYLESHEET"
 
24
TYPE="text/css"
 
25
HREF="stylesheet.css"><META
 
26
HTTP-EQUIV="Content-Type"
 
27
CONTENT="text/html; charset=ISO-8859-1"><META
 
28
NAME="creation"
 
29
CONTENT="2011-04-27T21:20:33"></HEAD
 
30
><BODY
 
31
CLASS="SECT1"
 
32
><DIV
 
33
CLASS="NAVHEADER"
 
34
><TABLE
 
35
SUMMARY="Header navigation table"
 
36
WIDTH="100%"
 
37
BORDER="0"
 
38
CELLPADDING="0"
 
39
CELLSPACING="0"
 
40
><TR
 
41
><TH
 
42
COLSPAN="5"
 
43
ALIGN="center"
 
44
VALIGN="bottom"
 
45
><A
 
46
HREF="index.html"
 
47
>PostgreSQL 9.1beta1 Documentation</A
 
48
></TH
 
49
></TR
 
50
><TR
 
51
><TD
 
52
WIDTH="10%"
 
53
ALIGN="left"
 
54
VALIGN="top"
 
55
><A
 
56
TITLE="Expressions"
 
57
HREF="plpgsql-expressions.html"
 
58
ACCESSKEY="P"
 
59
>Prev</A
 
60
></TD
 
61
><TD
 
62
WIDTH="10%"
 
63
ALIGN="left"
 
64
VALIGN="top"
 
65
><A
 
66
TITLE="PL/pgSQL - SQL Procedural Language"
 
67
HREF="plpgsql.html"
 
68
>Fast Backward</A
 
69
></TD
 
70
><TD
 
71
WIDTH="60%"
 
72
ALIGN="center"
 
73
VALIGN="bottom"
 
74
>Chapter 39. <SPAN
 
75
CLASS="APPLICATION"
 
76
>PL/pgSQL</SPAN
 
77
> - <ACRONYM
 
78
CLASS="ACRONYM"
 
79
>SQL</ACRONYM
 
80
> Procedural Language</TD
 
81
><TD
 
82
WIDTH="10%"
 
83
ALIGN="right"
 
84
VALIGN="top"
 
85
><A
 
86
TITLE="PL/pgSQL - SQL Procedural Language"
 
87
HREF="plpgsql.html"
 
88
>Fast Forward</A
 
89
></TD
 
90
><TD
 
91
WIDTH="10%"
 
92
ALIGN="right"
 
93
VALIGN="top"
 
94
><A
 
95
TITLE="Control Structures"
 
96
HREF="plpgsql-control-structures.html"
 
97
ACCESSKEY="N"
 
98
>Next</A
 
99
></TD
 
100
></TR
 
101
></TABLE
 
102
><HR
 
103
ALIGN="LEFT"
 
104
WIDTH="100%"></DIV
 
105
><DIV
 
106
CLASS="SECT1"
 
107
><H1
 
108
CLASS="SECT1"
 
109
><A
 
110
NAME="PLPGSQL-STATEMENTS"
 
111
>39.5. Basic Statements</A
 
112
></H1
 
113
><P
 
114
>    In this section and the following ones, we describe all the statement
 
115
    types that are explicitly understood by
 
116
    <SPAN
 
117
CLASS="APPLICATION"
 
118
>PL/pgSQL</SPAN
 
119
>.
 
120
    Anything not recognized as one of these statement types is presumed
 
121
    to be an SQL command and is sent to the main database engine to execute,
 
122
    as described in <A
 
123
HREF="plpgsql-statements.html#PLPGSQL-STATEMENTS-SQL-NORESULT"
 
124
>Section 39.5.2</A
 
125
>
 
126
    and <A
 
127
HREF="plpgsql-statements.html#PLPGSQL-STATEMENTS-SQL-ONEROW"
 
128
>Section 39.5.3</A
 
129
>.
 
130
   </P
 
131
><DIV
 
132
CLASS="SECT2"
 
133
><H2
 
134
CLASS="SECT2"
 
135
><A
 
136
NAME="PLPGSQL-STATEMENTS-ASSIGNMENT"
 
137
>39.5.1. Assignment</A
 
138
></H2
 
139
><P
 
140
>     An assignment of a value to a <SPAN
 
141
CLASS="APPLICATION"
 
142
>PL/pgSQL</SPAN
 
143
>
 
144
     variable is written as:
 
145
</P><PRE
 
146
CLASS="SYNOPSIS"
 
147
><TT
 
148
CLASS="REPLACEABLE"
 
149
><I
 
150
>variable</I
 
151
></TT
 
152
> := <TT
 
153
CLASS="REPLACEABLE"
 
154
><I
 
155
>expression</I
 
156
></TT
 
157
>;</PRE
 
158
><P>
 
159
     As explained previously, the expression in such a statement is evaluated
 
160
     by means of an SQL <TT
 
161
CLASS="COMMAND"
 
162
>SELECT</TT
 
163
> command sent to the main
 
164
     database engine.  The expression must yield a single value (possibly
 
165
     a row value, if the variable is a row or record variable).  The target
 
166
     variable can be a simple variable (optionally qualified with a block
 
167
     name), a field of a row or record variable, or an element of an array
 
168
     that is a simple variable or field.
 
169
    </P
 
170
><P
 
171
>     If the expression's result data type doesn't match the variable's
 
172
     data type, or the variable has a specific size/precision
 
173
     (like <TT
 
174
CLASS="TYPE"
 
175
>char(20)</TT
 
176
>), the result value will be implicitly
 
177
     converted by the <SPAN
 
178
CLASS="APPLICATION"
 
179
>PL/pgSQL</SPAN
 
180
> interpreter using
 
181
     the result type's output-function and
 
182
     the variable type's input-function. Note that this could potentially
 
183
     result in run-time errors generated by the input function, if the
 
184
     string form of the result value is not acceptable to the input function.
 
185
    </P
 
186
><P
 
187
>     Examples:
 
188
</P><PRE
 
189
CLASS="PROGRAMLISTING"
 
190
>tax := subtotal * 0.06;
 
191
my_record.user_id := 20;</PRE
 
192
><P>
 
193
    </P
 
194
></DIV
 
195
><DIV
 
196
CLASS="SECT2"
 
197
><H2
 
198
CLASS="SECT2"
 
199
><A
 
200
NAME="PLPGSQL-STATEMENTS-SQL-NORESULT"
 
201
>39.5.2. Executing a Command With No Result</A
 
202
></H2
 
203
><P
 
204
>     For any SQL command that does not return rows, for example
 
205
     <TT
 
206
CLASS="COMMAND"
 
207
>INSERT</TT
 
208
> without a <TT
 
209
CLASS="LITERAL"
 
210
>RETURNING</TT
 
211
> clause, you can
 
212
     execute the command within a <SPAN
 
213
CLASS="APPLICATION"
 
214
>PL/pgSQL</SPAN
 
215
> function
 
216
     just by writing the command.
 
217
    </P
 
218
><P
 
219
>     Any <SPAN
 
220
CLASS="APPLICATION"
 
221
>PL/pgSQL</SPAN
 
222
> variable name appearing
 
223
     in the command text is treated as a parameter, and then the
 
224
     current value of the variable is provided as the parameter value
 
225
     at run time.  This is exactly like the processing described earlier
 
226
     for expressions; for details see <A
 
227
HREF="plpgsql-implementation.html#PLPGSQL-VAR-SUBST"
 
228
>Section 39.10.1</A
 
229
>.
 
230
    </P
 
231
><P
 
232
>     When executing a SQL command in this way,
 
233
     <SPAN
 
234
CLASS="APPLICATION"
 
235
>PL/pgSQL</SPAN
 
236
> plans the command just once
 
237
     and re-uses the plan on subsequent executions, for the life of
 
238
     the database connection.  The implications of this are discussed
 
239
     in detail in <A
 
240
HREF="plpgsql-implementation.html#PLPGSQL-PLAN-CACHING"
 
241
>Section 39.10.2</A
 
242
>.
 
243
    </P
 
244
><P
 
245
>     Sometimes it is useful to evaluate an expression or <TT
 
246
CLASS="COMMAND"
 
247
>SELECT</TT
 
248
>
 
249
     query but discard the result, for example when calling a function
 
250
     that has side-effects but no useful result value.  To do
 
251
     this in <SPAN
 
252
CLASS="APPLICATION"
 
253
>PL/pgSQL</SPAN
 
254
>, use the
 
255
     <TT
 
256
CLASS="COMMAND"
 
257
>PERFORM</TT
 
258
> statement:
 
259
 
 
260
</P><PRE
 
261
CLASS="SYNOPSIS"
 
262
>PERFORM <TT
 
263
CLASS="REPLACEABLE"
 
264
><I
 
265
>query</I
 
266
></TT
 
267
>;</PRE
 
268
><P>
 
269
 
 
270
     This executes <TT
 
271
CLASS="REPLACEABLE"
 
272
><I
 
273
>query</I
 
274
></TT
 
275
> and discards the
 
276
     result.  Write the <TT
 
277
CLASS="REPLACEABLE"
 
278
><I
 
279
>query</I
 
280
></TT
 
281
> the same
 
282
     way you would write an SQL <TT
 
283
CLASS="COMMAND"
 
284
>SELECT</TT
 
285
> command, but replace the
 
286
     initial keyword <TT
 
287
CLASS="COMMAND"
 
288
>SELECT</TT
 
289
> with <TT
 
290
CLASS="COMMAND"
 
291
>PERFORM</TT
 
292
>.
 
293
     <SPAN
 
294
CLASS="APPLICATION"
 
295
>PL/pgSQL</SPAN
 
296
> variables will be
 
297
     substituted into the query just as for commands that return no result,
 
298
     and the plan is cached in the same way.  Also, the special variable
 
299
     <TT
 
300
CLASS="LITERAL"
 
301
>FOUND</TT
 
302
> is set to true if the query produced at
 
303
     least one row, or false if it produced no rows (see
 
304
     <A
 
305
HREF="plpgsql-statements.html#PLPGSQL-STATEMENTS-DIAGNOSTICS"
 
306
>Section 39.5.5</A
 
307
>).
 
308
    </P
 
309
><DIV
 
310
CLASS="NOTE"
 
311
><BLOCKQUOTE
 
312
CLASS="NOTE"
 
313
><P
 
314
><B
 
315
>Note: </B
 
316
>      One might expect that writing <TT
 
317
CLASS="COMMAND"
 
318
>SELECT</TT
 
319
> directly
 
320
      would accomplish this result, but at
 
321
      present the only accepted way to do it is
 
322
      <TT
 
323
CLASS="COMMAND"
 
324
>PERFORM</TT
 
325
>.  A SQL command that can return rows,
 
326
      such as <TT
 
327
CLASS="COMMAND"
 
328
>SELECT</TT
 
329
>, will be rejected as an error
 
330
      unless it has an <TT
 
331
CLASS="LITERAL"
 
332
>INTO</TT
 
333
> clause as discussed in the
 
334
      next section.
 
335
     </P
 
336
></BLOCKQUOTE
 
337
></DIV
 
338
><P
 
339
>     An example:
 
340
</P><PRE
 
341
CLASS="PROGRAMLISTING"
 
342
>PERFORM create_mv('cs_session_page_requests_mv', my_query);</PRE
 
343
><P>
 
344
    </P
 
345
></DIV
 
346
><DIV
 
347
CLASS="SECT2"
 
348
><H2
 
349
CLASS="SECT2"
 
350
><A
 
351
NAME="PLPGSQL-STATEMENTS-SQL-ONEROW"
 
352
>39.5.3. Executing a Query with a Single-row Result</A
 
353
></H2
 
354
><P
 
355
>     The result of a SQL command yielding a single row (possibly of multiple
 
356
     columns) can be assigned to a record variable, row-type variable, or list
 
357
     of scalar variables.  This is done by writing the base SQL command and
 
358
     adding an <TT
 
359
CLASS="LITERAL"
 
360
>INTO</TT
 
361
> clause.  For example,
 
362
 
 
363
</P><PRE
 
364
CLASS="SYNOPSIS"
 
365
>SELECT <TT
 
366
CLASS="REPLACEABLE"
 
367
><I
 
368
>select_expressions</I
 
369
></TT
 
370
> INTO [<SPAN
 
371
CLASS="OPTIONAL"
 
372
>STRICT</SPAN
 
373
>] <TT
 
374
CLASS="REPLACEABLE"
 
375
><I
 
376
>target</I
 
377
></TT
 
378
> FROM ...;
 
379
INSERT ... RETURNING <TT
 
380
CLASS="REPLACEABLE"
 
381
><I
 
382
>expressions</I
 
383
></TT
 
384
> INTO [<SPAN
 
385
CLASS="OPTIONAL"
 
386
>STRICT</SPAN
 
387
>] <TT
 
388
CLASS="REPLACEABLE"
 
389
><I
 
390
>target</I
 
391
></TT
 
392
>;
 
393
UPDATE ... RETURNING <TT
 
394
CLASS="REPLACEABLE"
 
395
><I
 
396
>expressions</I
 
397
></TT
 
398
> INTO [<SPAN
 
399
CLASS="OPTIONAL"
 
400
>STRICT</SPAN
 
401
>] <TT
 
402
CLASS="REPLACEABLE"
 
403
><I
 
404
>target</I
 
405
></TT
 
406
>;
 
407
DELETE ... RETURNING <TT
 
408
CLASS="REPLACEABLE"
 
409
><I
 
410
>expressions</I
 
411
></TT
 
412
> INTO [<SPAN
 
413
CLASS="OPTIONAL"
 
414
>STRICT</SPAN
 
415
>] <TT
 
416
CLASS="REPLACEABLE"
 
417
><I
 
418
>target</I
 
419
></TT
 
420
>;</PRE
 
421
><P>
 
422
 
 
423
     where <TT
 
424
CLASS="REPLACEABLE"
 
425
><I
 
426
>target</I
 
427
></TT
 
428
> can be a record variable, a row
 
429
     variable, or a comma-separated list of simple variables and
 
430
     record/row fields.
 
431
     <SPAN
 
432
CLASS="APPLICATION"
 
433
>PL/pgSQL</SPAN
 
434
> variables will be
 
435
     substituted into the rest of the query, and the plan is cached,
 
436
     just as described above for commands that do not return rows.
 
437
     This works for <TT
 
438
CLASS="COMMAND"
 
439
>SELECT</TT
 
440
>,
 
441
     <TT
 
442
CLASS="COMMAND"
 
443
>INSERT</TT
 
444
>/<TT
 
445
CLASS="COMMAND"
 
446
>UPDATE</TT
 
447
>/<TT
 
448
CLASS="COMMAND"
 
449
>DELETE</TT
 
450
> with
 
451
     <TT
 
452
CLASS="LITERAL"
 
453
>RETURNING</TT
 
454
>, and utility commands that return row-set
 
455
     results (such as <TT
 
456
CLASS="COMMAND"
 
457
>EXPLAIN</TT
 
458
>).
 
459
     Except for the <TT
 
460
CLASS="LITERAL"
 
461
>INTO</TT
 
462
> clause, the SQL command is the same
 
463
     as it would be written outside <SPAN
 
464
CLASS="APPLICATION"
 
465
>PL/pgSQL</SPAN
 
466
>.
 
467
    </P
 
468
><DIV
 
469
CLASS="TIP"
 
470
><BLOCKQUOTE
 
471
CLASS="TIP"
 
472
><P
 
473
><B
 
474
>Tip: </B
 
475
>     Note that this interpretation of <TT
 
476
CLASS="COMMAND"
 
477
>SELECT</TT
 
478
> with <TT
 
479
CLASS="LITERAL"
 
480
>INTO</TT
 
481
>
 
482
     is quite different from <SPAN
 
483
CLASS="PRODUCTNAME"
 
484
>PostgreSQL</SPAN
 
485
>'s regular
 
486
     <TT
 
487
CLASS="COMMAND"
 
488
>SELECT INTO</TT
 
489
> command, wherein the <TT
 
490
CLASS="LITERAL"
 
491
>INTO</TT
 
492
>
 
493
     target is a newly created table.  If you want to create a table from a
 
494
     <TT
 
495
CLASS="COMMAND"
 
496
>SELECT</TT
 
497
> result inside a
 
498
     <SPAN
 
499
CLASS="APPLICATION"
 
500
>PL/pgSQL</SPAN
 
501
> function, use the syntax
 
502
     <TT
 
503
CLASS="COMMAND"
 
504
>CREATE TABLE ... AS SELECT</TT
 
505
>.
 
506
    </P
 
507
></BLOCKQUOTE
 
508
></DIV
 
509
><P
 
510
>     If a row or a variable list is used as target, the query's result columns
 
511
     must exactly match the structure of the target as to number and data
 
512
     types, or else a run-time error
 
513
     occurs.  When a record variable is the target, it automatically
 
514
     configures itself to the row type of the query result columns.
 
515
    </P
 
516
><P
 
517
>     The <TT
 
518
CLASS="LITERAL"
 
519
>INTO</TT
 
520
> clause can appear almost anywhere in the SQL
 
521
     command.  Customarily it is written either just before or just after
 
522
     the list of <TT
 
523
CLASS="REPLACEABLE"
 
524
><I
 
525
>select_expressions</I
 
526
></TT
 
527
> in a
 
528
     <TT
 
529
CLASS="COMMAND"
 
530
>SELECT</TT
 
531
> command, or at the end of the command for other
 
532
     command types.  It is recommended that you follow this convention
 
533
     in case the <SPAN
 
534
CLASS="APPLICATION"
 
535
>PL/pgSQL</SPAN
 
536
> parser becomes
 
537
     stricter in future versions.
 
538
    </P
 
539
><P
 
540
>     If <TT
 
541
CLASS="LITERAL"
 
542
>STRICT</TT
 
543
> is not specified in the <TT
 
544
CLASS="LITERAL"
 
545
>INTO</TT
 
546
>
 
547
     clause, then <TT
 
548
CLASS="REPLACEABLE"
 
549
><I
 
550
>target</I
 
551
></TT
 
552
> will be set to the first
 
553
     row returned by the query, or to nulls if the query returned no rows.
 
554
     (Note that <SPAN
 
555
CLASS="QUOTE"
 
556
>"the first row"</SPAN
 
557
> is not
 
558
     well-defined unless you've used <TT
 
559
CLASS="LITERAL"
 
560
>ORDER BY</TT
 
561
>.)  Any result rows
 
562
     after the first row are discarded.
 
563
     You can check the special <TT
 
564
CLASS="LITERAL"
 
565
>FOUND</TT
 
566
> variable (see
 
567
     <A
 
568
HREF="plpgsql-statements.html#PLPGSQL-STATEMENTS-DIAGNOSTICS"
 
569
>Section 39.5.5</A
 
570
>) to
 
571
     determine whether a row was returned:
 
572
 
 
573
</P><PRE
 
574
CLASS="PROGRAMLISTING"
 
575
>SELECT * INTO myrec FROM emp WHERE empname = myname;
 
576
IF NOT FOUND THEN
 
577
    RAISE EXCEPTION 'employee % not found', myname;
 
578
END IF;</PRE
 
579
><P>
 
580
 
 
581
     If the <TT
 
582
CLASS="LITERAL"
 
583
>STRICT</TT
 
584
> option is specified, the query must
 
585
     return exactly one row or a run-time error will be reported, either
 
586
     <TT
 
587
CLASS="LITERAL"
 
588
>NO_DATA_FOUND</TT
 
589
> (no rows) or <TT
 
590
CLASS="LITERAL"
 
591
>TOO_MANY_ROWS</TT
 
592
>
 
593
     (more than one row). You can use an exception block if you wish
 
594
     to catch the error, for example:
 
595
 
 
596
</P><PRE
 
597
CLASS="PROGRAMLISTING"
 
598
>BEGIN
 
599
    SELECT * INTO STRICT myrec FROM emp WHERE empname = myname;
 
600
    EXCEPTION
 
601
        WHEN NO_DATA_FOUND THEN
 
602
            RAISE EXCEPTION 'employee % not found', myname;
 
603
        WHEN TOO_MANY_ROWS THEN
 
604
            RAISE EXCEPTION 'employee % not unique', myname;
 
605
END;</PRE
 
606
><P>
 
607
     Successful execution of a command with <TT
 
608
CLASS="LITERAL"
 
609
>STRICT</TT
 
610
>
 
611
     always sets <TT
 
612
CLASS="LITERAL"
 
613
>FOUND</TT
 
614
> to true.
 
615
    </P
 
616
><P
 
617
>     For <TT
 
618
CLASS="COMMAND"
 
619
>INSERT</TT
 
620
>/<TT
 
621
CLASS="COMMAND"
 
622
>UPDATE</TT
 
623
>/<TT
 
624
CLASS="COMMAND"
 
625
>DELETE</TT
 
626
> with
 
627
     <TT
 
628
CLASS="LITERAL"
 
629
>RETURNING</TT
 
630
>, <SPAN
 
631
CLASS="APPLICATION"
 
632
>PL/pgSQL</SPAN
 
633
> reports
 
634
     an error for more than one returned row, even when
 
635
     <TT
 
636
CLASS="LITERAL"
 
637
>STRICT</TT
 
638
> is not specified.  This is because there
 
639
     is no option such as <TT
 
640
CLASS="LITERAL"
 
641
>ORDER BY</TT
 
642
> with which to determine
 
643
     which affected row should be returned.
 
644
    </P
 
645
><DIV
 
646
CLASS="NOTE"
 
647
><BLOCKQUOTE
 
648
CLASS="NOTE"
 
649
><P
 
650
><B
 
651
>Note: </B
 
652
>      The <TT
 
653
CLASS="LITERAL"
 
654
>STRICT</TT
 
655
> option matches the behavior of
 
656
      Oracle PL/SQL's <TT
 
657
CLASS="COMMAND"
 
658
>SELECT INTO</TT
 
659
> and related statements.
 
660
     </P
 
661
></BLOCKQUOTE
 
662
></DIV
 
663
><P
 
664
>     To handle cases where you need to process multiple result rows
 
665
     from a SQL query, see <A
 
666
HREF="plpgsql-control-structures.html#PLPGSQL-RECORDS-ITERATING"
 
667
>Section 39.6.4</A
 
668
>.
 
669
    </P
 
670
></DIV
 
671
><DIV
 
672
CLASS="SECT2"
 
673
><H2
 
674
CLASS="SECT2"
 
675
><A
 
676
NAME="PLPGSQL-STATEMENTS-EXECUTING-DYN"
 
677
>39.5.4. Executing Dynamic Commands</A
 
678
></H2
 
679
><P
 
680
>     Oftentimes you will want to generate dynamic commands inside your
 
681
     <SPAN
 
682
CLASS="APPLICATION"
 
683
>PL/pgSQL</SPAN
 
684
> functions, that is, commands
 
685
     that will involve different tables or different data types each
 
686
     time they are executed.  <SPAN
 
687
CLASS="APPLICATION"
 
688
>PL/pgSQL</SPAN
 
689
>'s
 
690
     normal attempts to cache plans for commands (as discussed in
 
691
     <A
 
692
HREF="plpgsql-implementation.html#PLPGSQL-PLAN-CACHING"
 
693
>Section 39.10.2</A
 
694
>) will not work in such
 
695
     scenarios.  To handle this sort of problem, the
 
696
     <TT
 
697
CLASS="COMMAND"
 
698
>EXECUTE</TT
 
699
> statement is provided:
 
700
 
 
701
</P><PRE
 
702
CLASS="SYNOPSIS"
 
703
>EXECUTE <TT
 
704
CLASS="REPLACEABLE"
 
705
><I
 
706
>command-string</I
 
707
></TT
 
708
> [<SPAN
 
709
CLASS="OPTIONAL"
 
710
> INTO [<SPAN
 
711
CLASS="OPTIONAL"
 
712
>STRICT</SPAN
 
713
>] <TT
 
714
CLASS="REPLACEABLE"
 
715
><I
 
716
>target</I
 
717
></TT
 
718
> </SPAN
 
719
>] [<SPAN
 
720
CLASS="OPTIONAL"
 
721
> USING <TT
 
722
CLASS="REPLACEABLE"
 
723
><I
 
724
>expression</I
 
725
></TT
 
726
> [<SPAN
 
727
CLASS="OPTIONAL"
 
728
>, ... </SPAN
 
729
>] </SPAN
 
730
>];</PRE
 
731
><P>
 
732
 
 
733
     where <TT
 
734
CLASS="REPLACEABLE"
 
735
><I
 
736
>command-string</I
 
737
></TT
 
738
> is an expression
 
739
     yielding a string (of type <TT
 
740
CLASS="TYPE"
 
741
>text</TT
 
742
>) containing the
 
743
     command to be executed.  The optional <TT
 
744
CLASS="REPLACEABLE"
 
745
><I
 
746
>target</I
 
747
></TT
 
748
>
 
749
     is a record variable, a row variable, or a comma-separated list of
 
750
     simple variables and record/row fields, into which the results of
 
751
     the command will be stored.  The optional <TT
 
752
CLASS="LITERAL"
 
753
>USING</TT
 
754
> expressions
 
755
     supply values to be inserted into the command.
 
756
    </P
 
757
><P
 
758
>     No substitution of <SPAN
 
759
CLASS="APPLICATION"
 
760
>PL/pgSQL</SPAN
 
761
> variables is done on the
 
762
     computed command string.  Any required variable values must be inserted
 
763
     in the command string as it is constructed; or you can use parameters
 
764
     as described below.
 
765
    </P
 
766
><P
 
767
>     Also, there is no plan caching for commands executed via
 
768
     <TT
 
769
CLASS="COMMAND"
 
770
>EXECUTE</TT
 
771
>.  Instead, the
 
772
     command is prepared each time the statement is run. Thus the command
 
773
     string can be dynamically created within the function to perform
 
774
     actions on different tables and columns.
 
775
    </P
 
776
><P
 
777
>     The <TT
 
778
CLASS="LITERAL"
 
779
>INTO</TT
 
780
> clause specifies where the results of
 
781
     a SQL command returning rows should be assigned. If a row
 
782
     or variable list is provided, it must exactly match the structure
 
783
     of the query's results (when a
 
784
     record variable is used, it will configure itself to match the
 
785
     result structure automatically). If multiple rows are returned,
 
786
     only the first will be assigned to the <TT
 
787
CLASS="LITERAL"
 
788
>INTO</TT
 
789
>
 
790
     variable. If no rows are returned, NULL is assigned to the
 
791
     <TT
 
792
CLASS="LITERAL"
 
793
>INTO</TT
 
794
> variable(s). If no <TT
 
795
CLASS="LITERAL"
 
796
>INTO</TT
 
797
>
 
798
     clause is specified, the query results are discarded.
 
799
    </P
 
800
><P
 
801
>     If the <TT
 
802
CLASS="LITERAL"
 
803
>STRICT</TT
 
804
> option is given, an error is reported
 
805
     unless the query produces exactly one row.
 
806
    </P
 
807
><P
 
808
>     The command string can use parameter values, which are referenced
 
809
     in the command as <TT
 
810
CLASS="LITERAL"
 
811
>$1</TT
 
812
>, <TT
 
813
CLASS="LITERAL"
 
814
>$2</TT
 
815
>, etc.
 
816
     These symbols refer to values supplied in the <TT
 
817
CLASS="LITERAL"
 
818
>USING</TT
 
819
>
 
820
     clause.  This method is often preferable to inserting data values
 
821
     into the command string as text: it avoids run-time overhead of
 
822
     converting the values to text and back, and it is much less prone
 
823
     to SQL-injection attacks since there is no need for quoting or escaping.
 
824
     An example is:
 
825
</P><PRE
 
826
CLASS="PROGRAMLISTING"
 
827
>EXECUTE 'SELECT count(*) FROM mytable WHERE inserted_by = $1 AND inserted &lt;= $2'
 
828
   INTO c
 
829
   USING checked_user, checked_date;</PRE
 
830
><P>
 
831
    </P
 
832
><P
 
833
>     Note that parameter symbols can only be used for data values
 
834
     &mdash; if you want to use dynamically determined table or column
 
835
     names, you must insert them into the command string textually.
 
836
     For example, if the preceding query needed to be done against a
 
837
     dynamically selected table, you could do this:
 
838
</P><PRE
 
839
CLASS="PROGRAMLISTING"
 
840
>EXECUTE 'SELECT count(*) FROM '
 
841
    || tabname::regclass
 
842
    || ' WHERE inserted_by = $1 AND inserted &lt;= $2'
 
843
   INTO c
 
844
   USING checked_user, checked_date;</PRE
 
845
><P>
 
846
     Another restriction on parameter symbols is that they only work in
 
847
     <TT
 
848
CLASS="COMMAND"
 
849
>SELECT</TT
 
850
>, <TT
 
851
CLASS="COMMAND"
 
852
>INSERT</TT
 
853
>, <TT
 
854
CLASS="COMMAND"
 
855
>UPDATE</TT
 
856
>, and
 
857
     <TT
 
858
CLASS="COMMAND"
 
859
>DELETE</TT
 
860
> commands.  In other statement
 
861
     types (generically called utility statements), you must insert
 
862
     values textually even if they are just data values.
 
863
    </P
 
864
><P
 
865
>     An <TT
 
866
CLASS="COMMAND"
 
867
>EXECUTE</TT
 
868
> with a simple constant command string and some
 
869
     <TT
 
870
CLASS="LITERAL"
 
871
>USING</TT
 
872
> parameters, as in the first example above, is
 
873
     functionally equivalent to just writing the command directly in
 
874
     <SPAN
 
875
CLASS="APPLICATION"
 
876
>PL/pgSQL</SPAN
 
877
> and allowing replacement of
 
878
     <SPAN
 
879
CLASS="APPLICATION"
 
880
>PL/pgSQL</SPAN
 
881
> variables to happen automatically.
 
882
     The important difference is that <TT
 
883
CLASS="COMMAND"
 
884
>EXECUTE</TT
 
885
> will re-plan
 
886
     the command on each execution, generating a plan that is specific
 
887
     to the current parameter values; whereas
 
888
     <SPAN
 
889
CLASS="APPLICATION"
 
890
>PL/pgSQL</SPAN
 
891
> normally creates a generic plan
 
892
     and caches it for re-use.  In situations where the best plan depends
 
893
     strongly on the parameter values, <TT
 
894
CLASS="COMMAND"
 
895
>EXECUTE</TT
 
896
> can be
 
897
     significantly faster; while when the plan is not sensitive to parameter
 
898
     values, re-planning will be a waste.
 
899
    </P
 
900
><P
 
901
>     <TT
 
902
CLASS="COMMAND"
 
903
>SELECT INTO</TT
 
904
> is not currently supported within
 
905
     <TT
 
906
CLASS="COMMAND"
 
907
>EXECUTE</TT
 
908
>; instead, execute a plain <TT
 
909
CLASS="COMMAND"
 
910
>SELECT</TT
 
911
>
 
912
     command and specify <TT
 
913
CLASS="LITERAL"
 
914
>INTO</TT
 
915
> as part of the <TT
 
916
CLASS="COMMAND"
 
917
>EXECUTE</TT
 
918
>
 
919
     itself.
 
920
    </P
 
921
><DIV
 
922
CLASS="NOTE"
 
923
><BLOCKQUOTE
 
924
CLASS="NOTE"
 
925
><P
 
926
><B
 
927
>Note: </B
 
928
>     The <SPAN
 
929
CLASS="APPLICATION"
 
930
>PL/pgSQL</SPAN
 
931
>
 
932
     <TT
 
933
CLASS="COMMAND"
 
934
>EXECUTE</TT
 
935
> statement is not related to the
 
936
     <A
 
937
HREF="sql-execute.html"
 
938
>EXECUTE</A
 
939
> SQL
 
940
     statement supported by the
 
941
     <SPAN
 
942
CLASS="PRODUCTNAME"
 
943
>PostgreSQL</SPAN
 
944
> server. The server's
 
945
     <TT
 
946
CLASS="COMMAND"
 
947
>EXECUTE</TT
 
948
> statement cannot be used directly within
 
949
     <SPAN
 
950
CLASS="APPLICATION"
 
951
>PL/pgSQL</SPAN
 
952
> functions (and is not needed).
 
953
    </P
 
954
></BLOCKQUOTE
 
955
></DIV
 
956
><DIV
 
957
CLASS="EXAMPLE"
 
958
><A
 
959
NAME="PLPGSQL-QUOTE-LITERAL-EXAMPLE"
 
960
></A
 
961
><P
 
962
><B
 
963
>Example 39-1. Quoting Values In Dynamic Queries</B
 
964
></P
 
965
><P
 
966
>     When working with dynamic commands you will often have to handle escaping
 
967
     of single quotes.  The recommended method for quoting fixed text in your
 
968
     function body is dollar quoting.  (If you have legacy code that does
 
969
     not use dollar quoting, please refer to the
 
970
     overview in <A
 
971
HREF="plpgsql-development-tips.html#PLPGSQL-QUOTE-TIPS"
 
972
>Section 39.11.1</A
 
973
>, which can save you
 
974
     some effort when translating said code to a more reasonable scheme.)
 
975
    </P
 
976
><P
 
977
>     Dynamic values that are to be inserted into the constructed
 
978
     query require careful handling since they might themselves contain
 
979
     quote characters.
 
980
     An example (this assumes that you are using dollar quoting for the
 
981
     function as a whole, so the quote marks need not be doubled):
 
982
</P><PRE
 
983
CLASS="PROGRAMLISTING"
 
984
>EXECUTE 'UPDATE tbl SET '
 
985
        || quote_ident(colname)
 
986
        || ' = '
 
987
        || quote_literal(newvalue)
 
988
        || ' WHERE key = '
 
989
        || quote_literal(keyvalue);</PRE
 
990
><P>
 
991
    </P
 
992
><P
 
993
>     This example demonstrates the use of the
 
994
     <CODE
 
995
CLASS="FUNCTION"
 
996
>quote_ident</CODE
 
997
> and
 
998
     <CODE
 
999
CLASS="FUNCTION"
 
1000
>quote_literal</CODE
 
1001
> functions (see <A
 
1002
HREF="functions-string.html"
 
1003
>Section 9.4</A
 
1004
>).  For safety, expressions containing column
 
1005
     or table identifiers should be passed through
 
1006
     <CODE
 
1007
CLASS="FUNCTION"
 
1008
>quote_ident</CODE
 
1009
> before insertion in a dynamic query.
 
1010
     Expressions containing values that should be literal strings in the
 
1011
     constructed command should be passed through <CODE
 
1012
CLASS="FUNCTION"
 
1013
>quote_literal</CODE
 
1014
>.
 
1015
     These functions take the appropriate steps to return the input text
 
1016
     enclosed in double or single quotes respectively, with any embedded
 
1017
     special characters properly escaped.
 
1018
    </P
 
1019
><P
 
1020
>     Because <CODE
 
1021
CLASS="FUNCTION"
 
1022
>quote_literal</CODE
 
1023
> is labelled
 
1024
     <TT
 
1025
CLASS="LITERAL"
 
1026
>STRICT</TT
 
1027
>, it will always return null when called with a
 
1028
     null argument.  In the above example, if <TT
 
1029
CLASS="LITERAL"
 
1030
>newvalue</TT
 
1031
> or
 
1032
     <TT
 
1033
CLASS="LITERAL"
 
1034
>keyvalue</TT
 
1035
> were null, the entire dynamic query string would
 
1036
     become null, leading to an error from <TT
 
1037
CLASS="COMMAND"
 
1038
>EXECUTE</TT
 
1039
>.
 
1040
     You can avoid this problem by using the <CODE
 
1041
CLASS="FUNCTION"
 
1042
>quote_nullable</CODE
 
1043
>
 
1044
     function, which works the same as <CODE
 
1045
CLASS="FUNCTION"
 
1046
>quote_literal</CODE
 
1047
> except that
 
1048
     when called with a null argument it returns the string <TT
 
1049
CLASS="LITERAL"
 
1050
>NULL</TT
 
1051
>.
 
1052
     For example,
 
1053
</P><PRE
 
1054
CLASS="PROGRAMLISTING"
 
1055
>EXECUTE 'UPDATE tbl SET '
 
1056
        || quote_ident(colname)
 
1057
        || ' = '
 
1058
        || quote_nullable(newvalue)
 
1059
        || ' WHERE key = '
 
1060
        || quote_nullable(keyvalue);</PRE
 
1061
><P>
 
1062
     If you are dealing with values that might be null, you should usually
 
1063
     use <CODE
 
1064
CLASS="FUNCTION"
 
1065
>quote_nullable</CODE
 
1066
> in place of <CODE
 
1067
CLASS="FUNCTION"
 
1068
>quote_literal</CODE
 
1069
>.
 
1070
    </P
 
1071
><P
 
1072
>     As always, care must be taken to ensure that null values in a query do
 
1073
     not deliver unintended results.  For example the <TT
 
1074
CLASS="LITERAL"
 
1075
>WHERE</TT
 
1076
> clause
 
1077
</P><PRE
 
1078
CLASS="PROGRAMLISTING"
 
1079
>'WHERE key = ' || quote_nullable(keyvalue)</PRE
 
1080
><P>
 
1081
     will never succeed if <TT
 
1082
CLASS="LITERAL"
 
1083
>keyvalue</TT
 
1084
> is null, because the
 
1085
     result of using the equality operator <TT
 
1086
CLASS="LITERAL"
 
1087
>=</TT
 
1088
> with a null operand
 
1089
     is always null.  If you wish null to work like an ordinary key value,
 
1090
     you would need to rewrite the above as
 
1091
</P><PRE
 
1092
CLASS="PROGRAMLISTING"
 
1093
>'WHERE key IS NOT DISTINCT FROM ' || quote_nullable(keyvalue)</PRE
 
1094
><P>
 
1095
     (At present, <TT
 
1096
CLASS="LITERAL"
 
1097
>IS NOT DISTINCT FROM</TT
 
1098
> is handled much less
 
1099
     efficiently than <TT
 
1100
CLASS="LITERAL"
 
1101
>=</TT
 
1102
>, so don't do this unless you must.
 
1103
     See <A
 
1104
HREF="functions-comparison.html"
 
1105
>Section 9.2</A
 
1106
> for
 
1107
     more information on nulls and <TT
 
1108
CLASS="LITERAL"
 
1109
>IS DISTINCT</TT
 
1110
>.)
 
1111
    </P
 
1112
><P
 
1113
>     Note that dollar quoting is only useful for quoting fixed text.
 
1114
     It would be a very bad idea to try to write this example as:
 
1115
</P><PRE
 
1116
CLASS="PROGRAMLISTING"
 
1117
>EXECUTE 'UPDATE tbl SET '
 
1118
        || quote_ident(colname)
 
1119
        || ' = $$'
 
1120
        || newvalue
 
1121
        || '$$ WHERE key = '
 
1122
        || quote_literal(keyvalue);</PRE
 
1123
><P>
 
1124
     because it would break if the contents of <TT
 
1125
CLASS="LITERAL"
 
1126
>newvalue</TT
 
1127
>
 
1128
     happened to contain <TT
 
1129
CLASS="LITERAL"
 
1130
>$$</TT
 
1131
>.  The same objection would
 
1132
     apply to any other dollar-quoting delimiter you might pick.
 
1133
     So, to safely quote text that is not known in advance, you
 
1134
     <SPAN
 
1135
CLASS="emphasis"
 
1136
><I
 
1137
CLASS="EMPHASIS"
 
1138
>must</I
 
1139
></SPAN
 
1140
> use <CODE
 
1141
CLASS="FUNCTION"
 
1142
>quote_literal</CODE
 
1143
>,
 
1144
     <CODE
 
1145
CLASS="FUNCTION"
 
1146
>quote_nullable</CODE
 
1147
>, or <CODE
 
1148
CLASS="FUNCTION"
 
1149
>quote_ident</CODE
 
1150
>, as appropriate.
 
1151
    </P
 
1152
><P
 
1153
>     Dynamic SQL statements can also be safely constructed using the
 
1154
     <CODE
 
1155
CLASS="FUNCTION"
 
1156
>format</CODE
 
1157
> function (see <A
 
1158
HREF="functions-string.html"
 
1159
>Section 9.4</A
 
1160
>). For example:
 
1161
</P><PRE
 
1162
CLASS="PROGRAMLISTING"
 
1163
>EXECUTE format('UPDATE tbl SET %I = %L WHERE key = %L', colname, newvalue, keyvalue);</PRE
 
1164
><P>
 
1165
     The <CODE
 
1166
CLASS="FUNCTION"
 
1167
>format</CODE
 
1168
> function can be used in conjunction with
 
1169
     the <TT
 
1170
CLASS="LITERAL"
 
1171
>USING</TT
 
1172
> clause:
 
1173
</P><PRE
 
1174
CLASS="PROGRAMLISTING"
 
1175
>EXECUTE format('UPDATE tbl SET %I = $1 WHERE key = $2', colname)
 
1176
   USING newvalue, keyvalue;</PRE
 
1177
><P>
 
1178
     This form is more efficient, because the parameters
 
1179
     <TT
 
1180
CLASS="LITERAL"
 
1181
>newvalue</TT
 
1182
> and <TT
 
1183
CLASS="LITERAL"
 
1184
>keyvalue</TT
 
1185
> are not
 
1186
     converted to text.
 
1187
    </P
 
1188
></DIV
 
1189
><P
 
1190
>     A much larger example of a dynamic command and
 
1191
     <TT
 
1192
CLASS="COMMAND"
 
1193
>EXECUTE</TT
 
1194
> can be seen in <A
 
1195
HREF="plpgsql-porting.html#PLPGSQL-PORTING-EX2"
 
1196
>Example 39-8</A
 
1197
>, which builds and executes a
 
1198
     <TT
 
1199
CLASS="COMMAND"
 
1200
>CREATE FUNCTION</TT
 
1201
> command to define a new function.
 
1202
    </P
 
1203
></DIV
 
1204
><DIV
 
1205
CLASS="SECT2"
 
1206
><H2
 
1207
CLASS="SECT2"
 
1208
><A
 
1209
NAME="PLPGSQL-STATEMENTS-DIAGNOSTICS"
 
1210
>39.5.5. Obtaining the Result Status</A
 
1211
></H2
 
1212
><P
 
1213
>     There are several ways to determine the effect of a command. The
 
1214
     first method is to use the <TT
 
1215
CLASS="COMMAND"
 
1216
>GET DIAGNOSTICS</TT
 
1217
>
 
1218
     command, which has the form:
 
1219
 
 
1220
</P><PRE
 
1221
CLASS="SYNOPSIS"
 
1222
>GET DIAGNOSTICS <TT
 
1223
CLASS="REPLACEABLE"
 
1224
><I
 
1225
>variable</I
 
1226
></TT
 
1227
> = <TT
 
1228
CLASS="REPLACEABLE"
 
1229
><I
 
1230
>item</I
 
1231
></TT
 
1232
> [<SPAN
 
1233
CLASS="OPTIONAL"
 
1234
> , ... </SPAN
 
1235
>];</PRE
 
1236
><P>
 
1237
 
 
1238
     This command allows retrieval of system status indicators.  Each
 
1239
     <TT
 
1240
CLASS="REPLACEABLE"
 
1241
><I
 
1242
>item</I
 
1243
></TT
 
1244
> is a key word identifying a state
 
1245
     value to be assigned to the specified variable (which should be
 
1246
     of the right data type to receive it).  The currently available
 
1247
     status items are <TT
 
1248
CLASS="VARNAME"
 
1249
>ROW_COUNT</TT
 
1250
>, the number of rows
 
1251
     processed by the last <ACRONYM
 
1252
CLASS="ACRONYM"
 
1253
>SQL</ACRONYM
 
1254
> command sent to
 
1255
     the <ACRONYM
 
1256
CLASS="ACRONYM"
 
1257
>SQL</ACRONYM
 
1258
> engine, and <TT
 
1259
CLASS="VARNAME"
 
1260
>RESULT_OID</TT
 
1261
>,
 
1262
     the OID of the last row inserted by the most recent
 
1263
     <ACRONYM
 
1264
CLASS="ACRONYM"
 
1265
>SQL</ACRONYM
 
1266
> command.  Note that <TT
 
1267
CLASS="VARNAME"
 
1268
>RESULT_OID</TT
 
1269
>
 
1270
     is only useful after an <TT
 
1271
CLASS="COMMAND"
 
1272
>INSERT</TT
 
1273
> command into a
 
1274
     table containing OIDs.
 
1275
    </P
 
1276
><P
 
1277
>     An example:
 
1278
</P><PRE
 
1279
CLASS="PROGRAMLISTING"
 
1280
>GET DIAGNOSTICS integer_var = ROW_COUNT;</PRE
 
1281
><P>
 
1282
    </P
 
1283
><P
 
1284
>     The second method to determine the effects of a command is to check the
 
1285
     special variable named <TT
 
1286
CLASS="LITERAL"
 
1287
>FOUND</TT
 
1288
>, which is of
 
1289
     type <TT
 
1290
CLASS="TYPE"
 
1291
>boolean</TT
 
1292
>.  <TT
 
1293
CLASS="LITERAL"
 
1294
>FOUND</TT
 
1295
> starts out
 
1296
     false within each <SPAN
 
1297
CLASS="APPLICATION"
 
1298
>PL/pgSQL</SPAN
 
1299
> function call.
 
1300
     It is set by each of the following types of statements:
 
1301
 
 
1302
         <P
 
1303
></P
 
1304
></P><UL
 
1305
><LI
 
1306
><P
 
1307
>            A <TT
 
1308
CLASS="COMMAND"
 
1309
>SELECT INTO</TT
 
1310
> statement sets
 
1311
            <TT
 
1312
CLASS="LITERAL"
 
1313
>FOUND</TT
 
1314
> true if a row is assigned, false if no
 
1315
            row is returned.
 
1316
           </P
 
1317
></LI
 
1318
><LI
 
1319
><P
 
1320
>            A <TT
 
1321
CLASS="COMMAND"
 
1322
>PERFORM</TT
 
1323
> statement sets <TT
 
1324
CLASS="LITERAL"
 
1325
>FOUND</TT
 
1326
>
 
1327
            true if it produces (and discards) one or more rows, false if
 
1328
            no row is produced.
 
1329
           </P
 
1330
></LI
 
1331
><LI
 
1332
><P
 
1333
>            <TT
 
1334
CLASS="COMMAND"
 
1335
>UPDATE</TT
 
1336
>, <TT
 
1337
CLASS="COMMAND"
 
1338
>INSERT</TT
 
1339
>, and <TT
 
1340
CLASS="COMMAND"
 
1341
>DELETE</TT
 
1342
>
 
1343
            statements set <TT
 
1344
CLASS="LITERAL"
 
1345
>FOUND</TT
 
1346
> true if at least one
 
1347
            row is affected, false if no row is affected.
 
1348
           </P
 
1349
></LI
 
1350
><LI
 
1351
><P
 
1352
>            A <TT
 
1353
CLASS="COMMAND"
 
1354
>FETCH</TT
 
1355
> statement sets <TT
 
1356
CLASS="LITERAL"
 
1357
>FOUND</TT
 
1358
>
 
1359
            true if it returns a row, false if no row is returned.
 
1360
           </P
 
1361
></LI
 
1362
><LI
 
1363
><P
 
1364
>            A <TT
 
1365
CLASS="COMMAND"
 
1366
>MOVE</TT
 
1367
> statement sets <TT
 
1368
CLASS="LITERAL"
 
1369
>FOUND</TT
 
1370
>
 
1371
            true if it successfully repositions the cursor, false otherwise.
 
1372
           </P
 
1373
></LI
 
1374
><LI
 
1375
><P
 
1376
>            A <TT
 
1377
CLASS="COMMAND"
 
1378
>FOR</TT
 
1379
> or <TT
 
1380
CLASS="COMMAND"
 
1381
>FOREACH</TT
 
1382
> statement sets
 
1383
            <TT
 
1384
CLASS="LITERAL"
 
1385
>FOUND</TT
 
1386
> true
 
1387
            if it iterates one or more times, else false.
 
1388
            <TT
 
1389
CLASS="LITERAL"
 
1390
>FOUND</TT
 
1391
> is set this way when the
 
1392
            loop exits; inside the execution of the loop,
 
1393
            <TT
 
1394
CLASS="LITERAL"
 
1395
>FOUND</TT
 
1396
> is not modified by the
 
1397
            loop statement, although it might be changed by the
 
1398
            execution of other statements within the loop body.
 
1399
           </P
 
1400
></LI
 
1401
><LI
 
1402
><P
 
1403
>            <TT
 
1404
CLASS="COMMAND"
 
1405
>RETURN QUERY</TT
 
1406
> and <TT
 
1407
CLASS="COMMAND"
 
1408
>RETURN QUERY
 
1409
            EXECUTE</TT
 
1410
> statements set <TT
 
1411
CLASS="LITERAL"
 
1412
>FOUND</TT
 
1413
>
 
1414
            true if the query returns at least one row, false if no row
 
1415
            is returned.
 
1416
           </P
 
1417
></LI
 
1418
></UL
 
1419
><P>
 
1420
 
 
1421
     Other <SPAN
 
1422
CLASS="APPLICATION"
 
1423
>PL/pgSQL</SPAN
 
1424
> statements do not change
 
1425
     the state of <TT
 
1426
CLASS="LITERAL"
 
1427
>FOUND</TT
 
1428
>.
 
1429
     Note in particular that <TT
 
1430
CLASS="COMMAND"
 
1431
>EXECUTE</TT
 
1432
>
 
1433
     changes the output of <TT
 
1434
CLASS="COMMAND"
 
1435
>GET DIAGNOSTICS</TT
 
1436
>, but
 
1437
     does not change <TT
 
1438
CLASS="LITERAL"
 
1439
>FOUND</TT
 
1440
>.
 
1441
    </P
 
1442
><P
 
1443
>     <TT
 
1444
CLASS="LITERAL"
 
1445
>FOUND</TT
 
1446
> is a local variable within each
 
1447
     <SPAN
 
1448
CLASS="APPLICATION"
 
1449
>PL/pgSQL</SPAN
 
1450
> function; any changes to it
 
1451
     affect only the current function.
 
1452
    </P
 
1453
></DIV
 
1454
><DIV
 
1455
CLASS="SECT2"
 
1456
><H2
 
1457
CLASS="SECT2"
 
1458
><A
 
1459
NAME="PLPGSQL-STATEMENTS-NULL"
 
1460
>39.5.6. Doing Nothing At All</A
 
1461
></H2
 
1462
><P
 
1463
>     Sometimes a placeholder statement that does nothing is useful.
 
1464
     For example, it can indicate that one arm of an if/then/else
 
1465
     chain is deliberately empty.  For this purpose, use the
 
1466
     <TT
 
1467
CLASS="COMMAND"
 
1468
>NULL</TT
 
1469
> statement:
 
1470
 
 
1471
</P><PRE
 
1472
CLASS="SYNOPSIS"
 
1473
>NULL;</PRE
 
1474
><P>
 
1475
    </P
 
1476
><P
 
1477
>     For example, the following two fragments of code are equivalent:
 
1478
</P><PRE
 
1479
CLASS="PROGRAMLISTING"
 
1480
>BEGIN
 
1481
    y := x / 0;
 
1482
EXCEPTION
 
1483
    WHEN division_by_zero THEN
 
1484
        NULL;  -- ignore the error
 
1485
END;</PRE
 
1486
><P>
 
1487
 
 
1488
</P><PRE
 
1489
CLASS="PROGRAMLISTING"
 
1490
>BEGIN
 
1491
    y := x / 0;
 
1492
EXCEPTION
 
1493
    WHEN division_by_zero THEN  -- ignore the error
 
1494
END;</PRE
 
1495
><P>
 
1496
     Which is preferable is a matter of taste.
 
1497
    </P
 
1498
><DIV
 
1499
CLASS="NOTE"
 
1500
><BLOCKQUOTE
 
1501
CLASS="NOTE"
 
1502
><P
 
1503
><B
 
1504
>Note: </B
 
1505
>      In Oracle's PL/SQL, empty statement lists are not allowed, and so
 
1506
      <TT
 
1507
CLASS="COMMAND"
 
1508
>NULL</TT
 
1509
> statements are <SPAN
 
1510
CLASS="emphasis"
 
1511
><I
 
1512
CLASS="EMPHASIS"
 
1513
>required</I
 
1514
></SPAN
 
1515
> for situations
 
1516
      such as this.  <SPAN
 
1517
CLASS="APPLICATION"
 
1518
>PL/pgSQL</SPAN
 
1519
> allows you to
 
1520
      just write nothing, instead.
 
1521
     </P
 
1522
></BLOCKQUOTE
 
1523
></DIV
 
1524
></DIV
 
1525
></DIV
 
1526
><DIV
 
1527
CLASS="NAVFOOTER"
 
1528
><HR
 
1529
ALIGN="LEFT"
 
1530
WIDTH="100%"><TABLE
 
1531
SUMMARY="Footer navigation table"
 
1532
WIDTH="100%"
 
1533
BORDER="0"
 
1534
CELLPADDING="0"
 
1535
CELLSPACING="0"
 
1536
><TR
 
1537
><TD
 
1538
WIDTH="33%"
 
1539
ALIGN="left"
 
1540
VALIGN="top"
 
1541
><A
 
1542
HREF="plpgsql-expressions.html"
 
1543
ACCESSKEY="P"
 
1544
>Prev</A
 
1545
></TD
 
1546
><TD
 
1547
WIDTH="34%"
 
1548
ALIGN="center"
 
1549
VALIGN="top"
 
1550
><A
 
1551
HREF="index.html"
 
1552
ACCESSKEY="H"
 
1553
>Home</A
 
1554
></TD
 
1555
><TD
 
1556
WIDTH="33%"
 
1557
ALIGN="right"
 
1558
VALIGN="top"
 
1559
><A
 
1560
HREF="plpgsql-control-structures.html"
 
1561
ACCESSKEY="N"
 
1562
>Next</A
 
1563
></TD
 
1564
></TR
 
1565
><TR
 
1566
><TD
 
1567
WIDTH="33%"
 
1568
ALIGN="left"
 
1569
VALIGN="top"
 
1570
>Expressions</TD
 
1571
><TD
 
1572
WIDTH="34%"
 
1573
ALIGN="center"
 
1574
VALIGN="top"
 
1575
><A
 
1576
HREF="plpgsql.html"
 
1577
ACCESSKEY="U"
 
1578
>Up</A
 
1579
></TD
 
1580
><TD
 
1581
WIDTH="33%"
 
1582
ALIGN="right"
 
1583
VALIGN="top"
 
1584
>Control Structures</TD
 
1585
></TR
 
1586
></TABLE
 
1587
></DIV
 
1588
></BODY
 
1589
></HTML
 
1590
>
 
 
b'\\ No newline at end of file'