OSDN Git Service

FIRST REPOSITORY
[eos/hostdependOTHERS.git] / I386LINUX / util / I386LINUX / doc / postgresql / html / ecpg-variables.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >Using Host Variables</TITLE
6 ><META
7 NAME="GENERATOR"
8 CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
9 REV="MADE"
10 HREF="mailto:pgsql-docs@postgresql.org"><LINK
11 REL="HOME"
12 TITLE="PostgreSQL 7.4.1 Documentation"
13 HREF="index.html"><LINK
14 REL="UP"
15 TITLE="ECPG - Embedded SQL in C"
16 HREF="ecpg.html"><LINK
17 REL="PREVIOUS"
18 TITLE="Choosing a Connection"
19 HREF="ecpg-set-connection.html"><LINK
20 REL="NEXT"
21 TITLE="Dynamic SQL"
22 HREF="ecpg-dynamic.html"><LINK
23 REL="STYLESHEET"
24 TYPE="text/css"
25 HREF="stylesheet.css"><META
26 NAME="creation"
27 CONTENT="2003-12-22T03:48:47"></HEAD
28 ><BODY
29 CLASS="SECT1"
30 ><DIV
31 CLASS="NAVHEADER"
32 ><TABLE
33 SUMMARY="Header navigation table"
34 WIDTH="100%"
35 BORDER="0"
36 CELLPADDING="0"
37 CELLSPACING="0"
38 ><TR
39 ><TH
40 COLSPAN="5"
41 ALIGN="center"
42 VALIGN="bottom"
43 >PostgreSQL 7.4.1 Documentation</TH
44 ></TR
45 ><TR
46 ><TD
47 WIDTH="10%"
48 ALIGN="left"
49 VALIGN="top"
50 ><A
51 HREF="ecpg-set-connection.html"
52 ACCESSKEY="P"
53 >Prev</A
54 ></TD
55 ><TD
56 WIDTH="10%"
57 ALIGN="left"
58 VALIGN="top"
59 ><A
60 HREF="ecpg.html"
61 >Fast Backward</A
62 ></TD
63 ><TD
64 WIDTH="60%"
65 ALIGN="center"
66 VALIGN="bottom"
67 >Chapter 30. <SPAN
68 CLASS="APPLICATION"
69 >ECPG</SPAN
70 > - Embedded <ACRONYM
71 CLASS="ACRONYM"
72 >SQL</ACRONYM
73 > in C</TD
74 ><TD
75 WIDTH="10%"
76 ALIGN="right"
77 VALIGN="top"
78 ><A
79 HREF="ecpg.html"
80 >Fast Forward</A
81 ></TD
82 ><TD
83 WIDTH="10%"
84 ALIGN="right"
85 VALIGN="top"
86 ><A
87 HREF="ecpg-dynamic.html"
88 ACCESSKEY="N"
89 >Next</A
90 ></TD
91 ></TR
92 ></TABLE
93 ><HR
94 ALIGN="LEFT"
95 WIDTH="100%"></DIV
96 ><DIV
97 CLASS="SECT1"
98 ><H1
99 CLASS="SECT1"
100 ><A
101 NAME="ECPG-VARIABLES"
102 >30.6. Using Host Variables</A
103 ></H1
104 ><P
105 >   In <A
106 HREF="ecpg-commands.html"
107 >Section 30.4</A
108 > you saw how you can execute SQL
109    statements from an embedded SQL program.  Some of those statements
110    only used fixed values and did not provide a way to insert
111    user-supplied values into statements or have the program process
112    the values returned by the query.  Those kinds of statements are
113    not really useful in real applications.  This section explains in
114    detail how you can pass data between your C program and the
115    embedded SQL statements using a simple mechanism called
116    <I
117 CLASS="FIRSTTERM"
118 >host variables</I
119 >.
120   </P
121 ><DIV
122 CLASS="SECT2"
123 ><H2
124 CLASS="SECT2"
125 ><A
126 NAME="AEN23475"
127 >30.6.1. Overview</A
128 ></H2
129 ><P
130 >    Passing data between the C program and the SQL statements is
131     particularly simple in embedded SQL.  Instead of having the
132     program paste the data into the statement, which entails various
133     complications, such as properly quoting the value, you can simply
134     write the name of a C variable into the SQL statement, prefixed by
135     a colon.  For example:
136 </P><PRE
137 CLASS="PROGRAMLISTING"
138 >EXEC SQL INSERT INTO sometable VALUES (:v1, 'foo', :v2);</PRE
139 ><P>
140     This statements refers to two C variables named
141     <VAR
142 CLASS="VARNAME"
143 >v1</VAR
144 > and <VAR
145 CLASS="VARNAME"
146 >v2</VAR
147 > and also uses a
148     regular SQL string literal, to illustrate that you are not
149     restricted to use one kind of data or the other.
150    </P
151 ><P
152 >    This style of inserting C variables in SQL statements works
153     anywhere a value expression is expected in an SQL statement.  In
154     the SQL environment we call the references to C variables
155     <I
156 CLASS="FIRSTTERM"
157 >host variables</I
158 >.
159    </P
160 ></DIV
161 ><DIV
162 CLASS="SECT2"
163 ><H2
164 CLASS="SECT2"
165 ><A
166 NAME="AEN23483"
167 >30.6.2. Declare Sections</A
168 ></H2
169 ><P
170 >    To pass data from the program to the database, for example as
171     parameters in a query, or to pass data from the database back to
172     the program, the C variables that are intended to contain this
173     data need to be declared in specially marked sections, so the
174     embedded SQL preprocessor is made aware of them.
175    </P
176 ><P
177 >    This section starts with
178 </P><PRE
179 CLASS="PROGRAMLISTING"
180 >EXEC SQL BEGIN DECLARE SECTION;</PRE
181 ><P>
182     and ends with
183 </P><PRE
184 CLASS="PROGRAMLISTING"
185 >EXEC SQL END DECLARE SECTION;</PRE
186 ><P>
187     Between those lines, there must be normal C variable declarations,
188     such as
189 </P><PRE
190 CLASS="PROGRAMLISTING"
191 >int   x;
192 char  foo[16], bar[16];</PRE
193 ><P>
194     You can have as many declare sections in a program as you like.
195    </P
196 ><P
197 >    The declarations are also echoed to the output file as a normal C
198     variables, so there's no need to declare them again.  Variables
199     that are not intended to be used with SQL commands can be declared
200     normally outside these special sections.
201    </P
202 ><P
203 >    The definition of a structure or union also must be listed inside
204     a <TT
205 CLASS="LITERAL"
206 >DECLARE</TT
207 > section. Otherwise the preprocessor cannot
208     handle these types since it does not know the definition.
209    </P
210 ><P
211 >    The special type <TT
212 CLASS="TYPE"
213 >VARCHAR</TT
214
215     is converted into a named <TT
216 CLASS="TYPE"
217 >struct</TT
218 > for every variable. A
219     declaration like
220 </P><PRE
221 CLASS="PROGRAMLISTING"
222 >VARCHAR var[180];</PRE
223 ><P>
224     is converted into
225 </P><PRE
226 CLASS="PROGRAMLISTING"
227 >struct varchar_var { int len; char arr[180]; } var;</PRE
228 ><P>
229     This structure is suitable for interfacing with SQL datums of type
230     <TT
231 CLASS="TYPE"
232 >varchar</TT
233 >.
234    </P
235 ></DIV
236 ><DIV
237 CLASS="SECT2"
238 ><H2
239 CLASS="SECT2"
240 ><A
241 NAME="AEN23499"
242 >30.6.3. <TT
243 CLASS="COMMAND"
244 >SELECT INTO</TT
245 > and <TT
246 CLASS="COMMAND"
247 >FETCH INTO</TT
248 ></A
249 ></H2
250 ><P
251 >    Now you should be able to pass data generated by your program into
252     an SQL command.  But how do you retrieve the results of a query?
253     For that purpose, embedded SQL provides special variants of the
254     usual commands <TT
255 CLASS="COMMAND"
256 >SELECT</TT
257 > and
258     <TT
259 CLASS="COMMAND"
260 >FETCH</TT
261 >.  These commands have a special
262     <TT
263 CLASS="LITERAL"
264 >INTO</TT
265 > clause that specifies which host variables
266     the retrieved values are to be stored in.
267    </P
268 ><P
269 >    Here is an example:
270 </P><PRE
271 CLASS="PROGRAMLISTING"
272 >/*
273  * assume this table:
274  * CREATE TABLE test1 (a int, b varchar(50));
275  */
276
277 EXEC SQL BEGIN DECLARE SECTION;
278 int v1;
279 VARCHAR v2;
280 EXEC SQL END DECLARE SECTION;
281
282  ...
283
284 EXEC SQL SELECT a, b INTO :v1, :v2 FROM test;</PRE
285 ><P>
286     So the <TT
287 CLASS="LITERAL"
288 >INTO</TT
289 > clause appears between the select
290     list and the <TT
291 CLASS="LITERAL"
292 >FROM</TT
293 > clause.  The number of
294     elements in the select list and the list after
295     <TT
296 CLASS="LITERAL"
297 >INTO</TT
298 > (also called the target list) must be
299     equal.
300    </P
301 ><P
302 >    Here is an example using the command <TT
303 CLASS="COMMAND"
304 >FETCH</TT
305 >:
306 </P><PRE
307 CLASS="PROGRAMLISTING"
308 >EXEC SQL BEGIN DECLARE SECTION;
309 int v1;
310 VARCHAR v2;
311 EXEC SQL END DECLARE SECTION;
312
313  ...
314
315 EXEC SQL DECLARE foo CURSOR FOR SELECT a, b FROM test;
316
317  ...
318
319 do {
320     ...
321     EXEC SQL FETCH NEXT FROM foo INTO :v1, :v2;
322     ...
323 } while (...);</PRE
324 ><P>
325     Here the <TT
326 CLASS="LITERAL"
327 >INTO</TT
328 > clause appears after all the
329     normal clauses.
330    </P
331 ><P
332 >    Both of these methods only allow retrieving one row at a time.  If
333     you need to process result sets that potentially contain more than
334     one row, you need to use a cursor, as shown in the second example.
335    </P
336 ></DIV
337 ><DIV
338 CLASS="SECT2"
339 ><H2
340 CLASS="SECT2"
341 ><A
342 NAME="AEN23517"
343 >30.6.4. Indicators</A
344 ></H2
345 ><P
346 >    The examples above do not handle null values.  In fact, the
347     retrieval examples will raise an error if they fetch a null value
348     from the database.  To be able to pass null values to the database
349     or retrieve null values from the database, you need to append a
350     second host variable specification to each host variable that
351     contains data.  This second host variable is called the
352     <I
353 CLASS="FIRSTTERM"
354 >indicator</I
355 > and contains a flag that tells
356     whether the datums is null, in which case the value of the real
357     host variable is ignored.  Here is an example that handles the
358     retrieval of null values correctly:
359 </P><PRE
360 CLASS="PROGRAMLISTING"
361 >EXEC SQL BEGIN DECLARE SECTION;
362 VARCHAR val;
363 int val_ind;
364 EXEC SQL END DECLARE SECTION:
365
366  ...
367
368 EXEC SQL SELECT b INTO :val :val_ind FROM test1;</PRE
369 ><P>
370     The indicator variable <VAR
371 CLASS="VARNAME"
372 >val_ind</VAR
373 > will be zero if
374     the value was not null, and it will be negative if the value was
375     null.
376    </P
377 ><P
378 >    The indicator has another function: if the indicator value is
379     positive, it means that the value is not null, but it was
380     truncated when it was stored in the host variable.
381    </P
382 ></DIV
383 ></DIV
384 ><DIV
385 CLASS="NAVFOOTER"
386 ><HR
387 ALIGN="LEFT"
388 WIDTH="100%"><TABLE
389 SUMMARY="Footer navigation table"
390 WIDTH="100%"
391 BORDER="0"
392 CELLPADDING="0"
393 CELLSPACING="0"
394 ><TR
395 ><TD
396 WIDTH="33%"
397 ALIGN="left"
398 VALIGN="top"
399 ><A
400 HREF="ecpg-set-connection.html"
401 ACCESSKEY="P"
402 >Prev</A
403 ></TD
404 ><TD
405 WIDTH="34%"
406 ALIGN="center"
407 VALIGN="top"
408 ><A
409 HREF="index.html"
410 ACCESSKEY="H"
411 >Home</A
412 ></TD
413 ><TD
414 WIDTH="33%"
415 ALIGN="right"
416 VALIGN="top"
417 ><A
418 HREF="ecpg-dynamic.html"
419 ACCESSKEY="N"
420 >Next</A
421 ></TD
422 ></TR
423 ><TR
424 ><TD
425 WIDTH="33%"
426 ALIGN="left"
427 VALIGN="top"
428 >Choosing a Connection</TD
429 ><TD
430 WIDTH="34%"
431 ALIGN="center"
432 VALIGN="top"
433 ><A
434 HREF="ecpg.html"
435 ACCESSKEY="U"
436 >Up</A
437 ></TD
438 ><TD
439 WIDTH="33%"
440 ALIGN="right"
441 VALIGN="top"
442 >Dynamic SQL</TD
443 ></TR
444 ></TABLE
445 ></DIV
446 ></BODY
447 ></HTML
448 >