1
1
<!--
2
- $PostgreSQL: pgsql/doc/src/sgml/ref/prepare.sgml,v 1.17 2006/01/08 07:00:25 neilc Exp $
2
+ $PostgreSQL: pgsql/doc/src/sgml/ref/prepare.sgml,v 1.18 2006/01/15 22:18:46 neilc Exp $
3
3
PostgreSQL documentation
4
4
-->
5
5
@@ -25,7 +25,7 @@ PostgreSQL documentation
25
25
26
26
<refsynopsisdiv>
27
27
<synopsis>
28
- PREPARE <replaceable class="PARAMETER">plan_name </replaceable> [ (<replaceable class="PARAMETER">datatype</replaceable> [, ...] ) ] AS <replaceable class="PARAMETER">statement</replaceable>
28
+ PREPARE <replaceable class="PARAMETER">name </replaceable> [ (<replaceable class="PARAMETER">datatype</replaceable> [, ...] ) ] AS <replaceable class="PARAMETER">statement</replaceable>
29
29
</synopsis>
30
30
</refsynopsisdiv>
31
31
@@ -45,13 +45,15 @@ PREPARE <replaceable class="PARAMETER">plan_name</replaceable> [ (<replaceable c
45
45
46
46
<para>
47
47
Prepared statements can take parameters: values that are
48
- substituted into the statement when it is executed. To include
49
- parameters in a prepared statement, supply a list of data types in
50
- the <command>PREPARE</command> statement, and, in the statement to
51
- be prepared itself, refer to the parameters by position using
52
- <literal>$1</literal>, <literal>$2</literal>, etc. When executing
53
- the statement, specify the actual values for these parameters in
54
- the <command>EXECUTE</command> statement. Refer to <xref
48
+ substituted into the statement when it is executed. When creating
49
+ the prepared statement, refer to parameters by position, using
50
+ <literal>$1</>, <literal>$2</>, etc. A corresponding list of
51
+ parameter data types can optionally be specified. When a
52
+ parameter's data type is not specified or is declared as
53
+ <literal>unknown</literal>, the type is inferred from the context
54
+ in which the parameter is used (if possible). When executing the
55
+ statement, specify the actual values for these parameters in the
56
+ <command>EXECUTE</command> statement. Refer to <xref
55
57
linkend="sql-execute" endterm="sql-execute-title"> for more
56
58
information about that.
57
59
</para>
@@ -84,7 +86,7 @@ PREPARE <replaceable class="PARAMETER">plan_name</replaceable> [ (<replaceable c
84
86
85
87
<variablelist>
86
88
<varlistentry>
87
- <term><replaceable class="PARAMETER">plan_name </replaceable></term>
89
+ <term><replaceable class="PARAMETER">name </replaceable></term>
88
90
<listitem>
89
91
<para>
90
92
An arbitrary name given to this particular prepared
@@ -99,8 +101,11 @@ PREPARE <replaceable class="PARAMETER">plan_name</replaceable> [ (<replaceable c
99
101
<term><replaceable class="PARAMETER">datatype</replaceable></term>
100
102
<listitem>
101
103
<para>
102
- The data type of a parameter to the prepared statement. To
103
- refer to the parameters in the prepared statement itself, use
104
+ The data type of a parameter to the prepared statement. If the
105
+ data type of a particular parameter is unspecified or is
106
+ specified as <literal>unknown</literal>, it will be inferred
107
+ from the context in which the parameter is used. To refer to the
108
+ parameters in the prepared statement itself, use
104
109
<literal>$1</literal>, <literal>$2</literal>, etc.
105
110
</para>
106
111
</listitem>
@@ -155,8 +160,8 @@ PREPARE <replaceable class="PARAMETER">plan_name</replaceable> [ (<replaceable c
155
160
<refsect1 id="sql-prepare-examples">
156
161
<title id="sql-prepare-examples-title">Examples</title>
157
162
<para>
158
- Create a prepared query for an <command>INSERT</command> statement,
159
- and then execute it:
163
+ Create a prepared statement for an <command>INSERT</command>
164
+ statement, and then execute it:
160
165
<programlisting>
161
166
PREPARE fooplan (int, text, bool, numeric) AS
162
167
INSERT INTO foo VALUES($1, $2, $3, $4);
@@ -165,14 +170,17 @@ EXECUTE fooplan(1, 'Hunter Valley', 't', 200.00);
165
170
</para>
166
171
167
172
<para>
168
- Create a prepared query for a <command>SELECT</command> statement,
169
- and then execute it:
173
+ Create a prepared statement for a <command>SELECT</command>
174
+ statement, and then execute it:
170
175
<programlisting>
171
- PREPARE usrrptplan (int, date ) AS
176
+ PREPARE usrrptplan (int) AS
172
177
SELECT * FROM users u, logs l WHERE u.usrid=$1 AND u.usrid=l.usrid
173
178
AND l.date = $2;
174
179
EXECUTE usrrptplan(1, current_date);
175
180
</programlisting>
181
+
182
+ Note that the data type of the second parameter is not specified,
183
+ so it is inferred from the context in which <literal>$2</> is used.
176
184
</para>
177
185
</refsect1>
178
186
<refsect1>
0 commit comments