<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >Using SQL Descriptor Areas</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REV="MADE" HREF="mailto:pgsql-docs@postgresql.org"><LINK REL="HOME" TITLE="PostgreSQL 8.4.12 Documentation" HREF="index.html"><LINK REL="UP" TITLE="ECPG - Embedded SQL in C" HREF="ecpg.html"><LINK REL="PREVIOUS" TITLE="Informix compatibility mode" HREF="ecpg-informix-compat.html"><LINK REL="NEXT" TITLE="Error Handling" HREF="ecpg-errors.html"><LINK REL="STYLESHEET" TYPE="text/css" HREF="stylesheet.css"><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1"><META NAME="creation" CONTENT="2012-05-31T23:30:11"></HEAD ><BODY CLASS="SECT1" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="5" ALIGN="center" VALIGN="bottom" >PostgreSQL 8.4.12 Documentation</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="ecpg-informix-compat.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="ecpg.html" >Fast Backward</A ></TD ><TD WIDTH="60%" ALIGN="center" VALIGN="bottom" >Chapter 32. <SPAN CLASS="APPLICATION" >ECPG</SPAN > - Embedded <ACRONYM CLASS="ACRONYM" >SQL</ACRONYM > in C</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="top" ><A HREF="ecpg.html" >Fast Forward</A ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="top" ><A HREF="ecpg-errors.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="ECPG-DESCRIPTORS" >32.10. Using SQL Descriptor Areas</A ></H1 ><P > An SQL descriptor area is a more sophisticated method for processing the result of a <TT CLASS="COMMAND" >SELECT</TT > or <TT CLASS="COMMAND" >FETCH</TT > statement. An SQL descriptor area groups the data of one row of data together with metadata items into one data structure. The metadata is particularly useful when executing dynamic SQL statements, where the nature of the result columns might not be known ahead of time. </P ><P > An SQL descriptor area consists of a header, which contains information concerning the entire descriptor, and one or more item descriptor areas, which basically each describe one column in the result row. </P ><P > Before you can use an SQL descriptor area, you need to allocate one: </P><PRE CLASS="PROGRAMLISTING" >EXEC SQL ALLOCATE DESCRIPTOR <TT CLASS="REPLACEABLE" ><I >identifier</I ></TT >;</PRE ><P> The identifier serves as the <SPAN CLASS="QUOTE" >"variable name"</SPAN > of the descriptor area. When you don't need the descriptor anymore, you should deallocate it: </P><PRE CLASS="PROGRAMLISTING" >EXEC SQL DEALLOCATE DESCRIPTOR <TT CLASS="REPLACEABLE" ><I >identifier</I ></TT >;</PRE ><P> </P ><P > To use a descriptor area, specify it as the storage target in an <TT CLASS="LITERAL" >INTO</TT > clause, instead of listing host variables: </P><PRE CLASS="PROGRAMLISTING" >EXEC SQL FETCH NEXT FROM mycursor INTO DESCRIPTOR mydesc;</PRE ><P> </P ><P > Now how do you get the data out of the descriptor area? You can think of the descriptor area as a structure with named fields. To retrieve the value of a field from the header and store it into a host variable, use the following command: </P><PRE CLASS="PROGRAMLISTING" >EXEC SQL GET DESCRIPTOR <TT CLASS="REPLACEABLE" ><I >name</I ></TT > :<TT CLASS="REPLACEABLE" ><I >hostvar</I ></TT > = <TT CLASS="REPLACEABLE" ><I >field</I ></TT >;</PRE ><P> Currently, there is only one header field defined: <TT CLASS="REPLACEABLE" ><I >COUNT</I ></TT >, which tells how many item descriptor areas exist (that is, how many columns are contained in the result). The host variable needs to be of an integer type. To get a field from the item descriptor area, use the following command: </P><PRE CLASS="PROGRAMLISTING" >EXEC SQL GET DESCRIPTOR <TT CLASS="REPLACEABLE" ><I >name</I ></TT > VALUE <TT CLASS="REPLACEABLE" ><I >num</I ></TT > :<TT CLASS="REPLACEABLE" ><I >hostvar</I ></TT > = <TT CLASS="REPLACEABLE" ><I >field</I ></TT >;</PRE ><P> <TT CLASS="REPLACEABLE" ><I >num</I ></TT > can be a literal integer or a host variable containing an integer. Possible fields are: <P ></P ></P><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="LITERAL" >CARDINALITY</TT > (integer)</DT ><DD ><P > number of rows in the result set </P ></DD ><DT ><TT CLASS="LITERAL" >DATA</TT ></DT ><DD ><P > actual data item (therefore, the data type of this field depends on the query) </P ></DD ><DT ><TT CLASS="LITERAL" >DATETIME_INTERVAL_CODE</TT > (integer)</DT ><DD ><P > ? </P ></DD ><DT ><TT CLASS="LITERAL" >DATETIME_INTERVAL_PRECISION</TT > (integer)</DT ><DD ><P > not implemented </P ></DD ><DT ><TT CLASS="LITERAL" >INDICATOR</TT > (integer)</DT ><DD ><P > the indicator (indicating a null value or a value truncation) </P ></DD ><DT ><TT CLASS="LITERAL" >KEY_MEMBER</TT > (integer)</DT ><DD ><P > not implemented </P ></DD ><DT ><TT CLASS="LITERAL" >LENGTH</TT > (integer)</DT ><DD ><P > length of the datum in characters </P ></DD ><DT ><TT CLASS="LITERAL" >NAME</TT > (string)</DT ><DD ><P > name of the column </P ></DD ><DT ><TT CLASS="LITERAL" >NULLABLE</TT > (integer)</DT ><DD ><P > not implemented </P ></DD ><DT ><TT CLASS="LITERAL" >OCTET_LENGTH</TT > (integer)</DT ><DD ><P > length of the character representation of the datum in bytes </P ></DD ><DT ><TT CLASS="LITERAL" >PRECISION</TT > (integer)</DT ><DD ><P > precision (for type <TT CLASS="TYPE" >numeric</TT >) </P ></DD ><DT ><TT CLASS="LITERAL" >RETURNED_LENGTH</TT > (integer)</DT ><DD ><P > length of the datum in characters </P ></DD ><DT ><TT CLASS="LITERAL" >RETURNED_OCTET_LENGTH</TT > (integer)</DT ><DD ><P > length of the character representation of the datum in bytes </P ></DD ><DT ><TT CLASS="LITERAL" >SCALE</TT > (integer)</DT ><DD ><P > scale (for type <TT CLASS="TYPE" >numeric</TT >) </P ></DD ><DT ><TT CLASS="LITERAL" >TYPE</TT > (integer)</DT ><DD ><P > numeric code of the data type of the column </P ></DD ></DL ></DIV ><P> </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="ecpg-informix-compat.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="ecpg-errors.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><SPAN CLASS="PRODUCTNAME" >Informix</SPAN > compatibility mode</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ecpg.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Error Handling</TD ></TR ></TABLE ></DIV ></BODY ></HTML >