157 lines
10 KiB
HTML
157 lines
10 KiB
HTML
<?xml version="1.0" encoding="UTF-8"?>
|
|
<!DOCTYPE html
|
|
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html lang="en-us" xml:lang="en-us">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<meta name="security" content="public" />
|
|
<meta name="Robots" content="index,follow" />
|
|
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
|
|
<meta name="DC.Type" content="reference" />
|
|
<meta name="DC.Title" content="Define SQL descriptor areas in C and C++ applications that use SQL" />
|
|
<meta name="abstract" content="There are two types of SQL descriptor areas. One is defined with the ALLOCATE DESCRIPTOR statement. The other is defined using the SQL descriptor area (SQLDA) structure. In this topic, only the SQLDA form is discussed." />
|
|
<meta name="description" content="There are two types of SQL descriptor areas. One is defined with the ALLOCATE DESCRIPTOR statement. The other is defined using the SQL descriptor area (SQLDA) structure. In this topic, only the SQLDA form is discussed." />
|
|
<meta name="DC.subject" content="SQLDA (SQL descriptor area), C, C++, application program, SQLDA, C program, SQLDA, declaring, dynamic SQL coding, C++ program, SQLDA, declaring, dynamic SQL coding, dynamic SQL, coding in C, coding in C++" />
|
|
<meta name="keywords" content="SQLDA (SQL descriptor area), C, C++, application program, SQLDA, C program, SQLDA, declaring, dynamic SQL coding, C++ program, SQLDA, declaring, dynamic SQL coding, dynamic SQL, coding in C, coding in C++" />
|
|
<meta name="DC.Relation" scheme="URI" content="rzajpc.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="../sqlp/rbafydynmic.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="../db2/rbafzmstsqldda.htm" />
|
|
<meta name="copyright" content="(C) Copyright IBM Corporation 1998, 2006" />
|
|
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 1998, 2006" />
|
|
<meta name="DC.Format" content="XHTML" />
|
|
<meta name="DC.Identifier" content="rzajpsqldac" />
|
|
<meta name="DC.Language" content="en-us" />
|
|
<!-- All rights reserved. Licensed Materials Property of IBM -->
|
|
<!-- US Government Users Restricted Rights -->
|
|
<!-- Use, duplication or disclosure restricted by -->
|
|
<!-- GSA ADP Schedule Contract with IBM Corp. -->
|
|
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
|
|
<link rel="stylesheet" type="text/css" href="./ic.css" />
|
|
<title>Define SQL descriptor areas in C and C++ applications that use SQL</title>
|
|
</head>
|
|
<body id="rzajpsqldac"><a name="rzajpsqldac"><!-- --></a>
|
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
|
<h1 class="topictitle1">Define SQL descriptor areas in C and C++ applications that use SQL</h1>
|
|
<div><p><span><img src="./delta.gif" alt="Start of change" />There are two types of SQL descriptor areas.
|
|
One is defined with the ALLOCATE DESCRIPTOR statement. The other is defined
|
|
using the SQL descriptor area (SQLDA) structure. In this topic, only the SQLDA
|
|
form is discussed.<img src="./deltaend.gif" alt="End of change" /></span></p>
|
|
<div class="section"> <p><img src="./delta.gif" alt="Start of change" />The following statements can use an SQLDA:<img src="./deltaend.gif" alt="End of change" /></p>
|
|
<ul><li>EXECUTE…USING DESCRIPTOR <span class="synph"><span class="var">descriptor-name</span></span></li>
|
|
<li>FETCH…USING DESCRIPTOR <span class="synph"><span class="var">descriptor-name</span></span></li>
|
|
<li>OPEN…USING DESCRIPTOR <span class="synph"><span class="var">descriptor-name</span></span></li>
|
|
<li>DESCRIBE <span class="synph"><span class="var">statement-name</span></span> INTO <span class="synph"><span class="var">descriptor-name</span></span></li>
|
|
<li><img src="./delta.gif" alt="Start of change" />DESCRIBE INPUT <span class="synph"><span class="var">statement-name</span></span> INTO <span class="synph"><span class="var">descriptor-name</span></span><img src="./deltaend.gif" alt="End of change" /></li>
|
|
<li>DESCRIBE TABLE <span class="synph"><span class="var">host-variable</span></span> INTO <span class="synph"><span class="var">descriptor-name</span></span></li>
|
|
<li>PREPARE <span class="synph"><span class="var">statement-name</span></span> INTO <span class="synph"><span class="var">descriptor-name</span></span></li>
|
|
<li>CALL…USING DESCRIPTOR <span class="synph"><span class="var">descriptor-name</span></span></li>
|
|
</ul>
|
|
</div>
|
|
<div class="section"><p>Unlike the SQLCA, more than one SQLDA can be in the program, and
|
|
an SQLDA can have any valid name. The following list includes the statements
|
|
that require a SQLDA. You can code an SQLDA in a C or C++ program either directly
|
|
or by using the SQL INCLUDE statement. Using the SQL INCLUDE statement requests
|
|
the inclusion of a standard SQLDA declaration:</p>
|
|
<pre> EXEC SQL <strong>INCLUDE SQLDA</strong>;
|
|
</pre>
|
|
</div>
|
|
<div class="section"><p>A standard declaration includes only a structure definition with
|
|
the name 'sqlda'.</p>
|
|
</div>
|
|
<div class="section"><p>C and C++ declarations that are included for the SQLDA are:</p>
|
|
<pre> #ifndef SQLDASIZE
|
|
struct sqlda {
|
|
unsigned char sqldaid[8];
|
|
long sqldabc;
|
|
short sqln;
|
|
short sqld;
|
|
struct sqlvar {
|
|
short sqltype;
|
|
short sqllen;
|
|
unsigned char *sqldata;
|
|
short *sqlind;
|
|
struct sqlname {
|
|
short length;
|
|
unsigned char data[30];
|
|
} sqlname;
|
|
} sqlvar[1];
|
|
};
|
|
#define SQLDASIZE(n) (sizeof(struct sqlda) + (n-1)* sizeof(struct sqlvar))
|
|
#endif</pre>
|
|
</div>
|
|
<div class="section"><p>One benefit from using the INCLUDE SQLDA SQL statement is that
|
|
you also get the following macro definition:</p>
|
|
<pre>#define SQLDASIZE(n) (sizeof(struct sqlda) + (n-1)* sizeof(struc sqlvar))</pre>
|
|
<p>This macro makes it easy to allocate storage for an SQLDA with a specified
|
|
number of SQLVAR elements. In the following example, the SQLDASIZE macro is
|
|
used to allocate storage for an SQLDA with 20 SQLVAR elements. </p>
|
|
<pre> #include <stdlib.h>
|
|
EXEC SQL <strong>INCLUDE SQLDA</strong>;
|
|
|
|
struct sqlda *mydaptr;
|
|
short numvars = 20;
|
|
.
|
|
.
|
|
mydaptr = (struct sqlda *) malloc(SQLDASIZE(numvars));
|
|
mydaptr->sqln = 20;</pre>
|
|
</div>
|
|
<div class="section"><p>Here are other macro definitions that are included with the INCLUDE
|
|
SQLDA statement:</p>
|
|
<dl><dt class="dlterm">GETSQLDOUBLED(daptr)</dt>
|
|
<dd>Returns 1 if the SQLDA pointed to by daptr has been doubled, or 0 if it
|
|
has not been doubled. The SQLDA is doubled if the seventh byte in the SQLDAID
|
|
field is set to '2'.</dd>
|
|
<dt class="dlterm">SETSQLDOUBLED(daptr, newvalue)</dt>
|
|
<dd>Sets the seventh byte of SQLDAID to a newvalue.</dd>
|
|
<dt class="dlterm">GETSQLDALONGLEN(daptr,n)</dt>
|
|
<dd>Returns the length attribute of the nth entry in the SQLDA to which daptr
|
|
points. Use this only if the SQLDA was doubled and the nth SQLVAR entry has
|
|
a LOB data type.</dd>
|
|
<dt class="dlterm">SETSQLDALONGLEN(daptr,n,len)</dt>
|
|
<dd>Sets the SQLLONGLEN field of the SQLDA to which daptr points to len for
|
|
the nth entry. Use this only if the SQLDA was doubled and the nth SQLVAR entry
|
|
has a LOB datatype.</dd>
|
|
<dt class="dlterm">GETSQLDALENPTR(daptr,n)</dt>
|
|
<dd>Returns a pointer to the actual length of the data for the nth entry in
|
|
the SQLDA to which daptr points. The SQLDATALEN pointer field returns a pointer
|
|
to a long (4 byte) integer. If the SQLDATALEN pointer is zero, a NULL pointer
|
|
is returned. Use this only if the SQLDA has been doubled.</dd>
|
|
<dt class="dlterm">SETSQLDALENPTR(daptr,n,ptr)</dt>
|
|
<dd>Sets a pointer to the actual length of the data for the nth entry in the
|
|
SQLDA to which daptr points. Use this only if the SQLDA has been doubled.</dd>
|
|
</dl>
|
|
</div>
|
|
<div class="section"><p>When you have declared an SQLDA as a pointer, you must reference
|
|
it exactly as declared when you use it in an SQL statement, just as you would
|
|
for a host variable that was declared as a pointer. To avoid compiler errors,
|
|
the type of the value that is assigned to the sqldata field of the SQLDA must
|
|
be a pointer of unsigned character. This helps avoid compiler errors. The
|
|
type casting is only necessary for the EXECUTE, OPEN, CALL, and FETCH statements
|
|
where the application program is passing the address of the host variables
|
|
in the program. For example, if you declared a pointer to an SQLDA called
|
|
mydaptr, you would use it in a PREPARE statement as:</p>
|
|
<pre> EXEC SQL <strong>PREPARE</strong> mysname <strong>INTO</strong> :*mydaptr <strong>FROM</strong> :mysqlstring;</pre>
|
|
</div>
|
|
<div class="section"><p>SQLDA declarations can appear wherever a structure definition
|
|
is allowed. Normal C scope rules apply.</p>
|
|
</div>
|
|
<div class="section"><p>Dynamic SQL is an advanced programming technique.
|
|
With dynamic SQL, your program can develop and then run SQL statements while
|
|
the program is running. A SELECT statement with a variable SELECT list (that
|
|
is a list of the data to be returned as part of the query) that runs dynamically
|
|
requires an SQL descriptor area (SQLDA). This is because you will not know
|
|
in advance how many or what type of variables to allocate in order to receive
|
|
the results of the SELECT.</p>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="rzajpc.htm" title="This topic describes the unique application and coding requirements for embedding SQL statements in a C or C++ program.">Code SQL statements in C and C++ applications</a></div>
|
|
</div>
|
|
<div class="relinfo"><strong>Related information</strong><br />
|
|
<div><a href="../sqlp/rbafydynmic.htm">Dynamic SQL applications</a></div>
|
|
<div><a href="../db2/rbafzmstsqldda.htm">SQL descriptor area</a></div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html> |