Collection Contents Index Static and dynamic SQL Using stored procedures in Embedded SQL pdf/chap2.pdf

Programming Interfaces Guide
   CHAPTER 2. The Embedded SQL Interface     

The SQL descriptor area (SQLDA)


The SQLDA (SQL Descriptor Area) is an interface structure that is used for dynamic SQL statements. The structure passes information regarding host variables and SELECT statement results to and from the database. The SQLDA is defined in the header file sqlda.h.

For Info     There are functions in the database interface library or DLL that you can use to manage SQLDAs. For descriptions, see SQLDA management functions.

When host variables are used with static SQL statements, the preprocessor constructs a SQLDA for those host variables. It is this SQLDA that is actually passed to and from the database server.

Top of page  SQLDA fields and their meanings

The SQLDA fields have the following meanings:

Field

Description

sqldaid

An 8-byte character field that contains the string SQLDA as an identification of the SQLDA structure. This field helps in debugging, when you are looking at memory contents.

sqldabc

A long integer containing the length of the SQLDA structure.

sqln

The number of variable descriptors in the sqlvar array.

sqld

The number of variable descriptors which are valid (contain information describing a host variable). This field is set by the DESCRIBE statement, and sometimes by the programmer when supplying data to the database server.

sqlvar

An array of descriptors of type struct sqlvar, each describing a host variable.

Top of page  Host variable descriptions in the SQLDA

Each sqlvar structure in the SQLDA describes a host variable. The fields of the sqlvar structure have the following meanings:

Top of page  Length field values

The sqllen field length of the sqlvar structure in a SQLDA is used in three different kinds of interactions with the database server. The following tables detail each of these interactions. These tables list the interface constant types (the DT_ types) found in the sqldef.h header file. These constants would be placed in the SQLDA sqltype field. The types are described in Embedded SQL data types.

In static SQL, a SQLDA is still used but it is generated and completely filled in by the SQL preprocessor. In this static case, the tables give the correspondence between the static C language host variable types and the interface constants.

DESCRIBE 

The following table indicates the values of the sqllen and sqltype structure members returned by the DESCRIBE command for the various database types (both SELECT LIST and BIND VARIABLE DESCRIBE statements). In the case of a user-defined database data type, the base type is described.

Your program can use the types and lengths returned from a DESCRIBE, or you may use another type. The database server will perform type conversions between any two types. The memory pointed to by the sqldata field must correspond to the sqltype and sqllen fields.

Database field type

Embedded SQL type returned

Length returned on describe

CHAR(n)

DT_FIXCHAR

n

VARCHAR(n)

DT_VARCHAR

n

BINARY(n)

DT_BINARY

n

SMALLINT

DT_SMALLINT

2

INT

DT_INT

4

TINYINT

DT_TINYINT

1

DECIMAL(p,s)

DT_DECIMAL

high byte of length field in SQLDA set to p, and low byte set to s

REAL

DT_FLOAT

4

FLOAT

DT_FLOAT

4

DOUBLE

DT_DOUBLE

8

DATE

DT_DATE

length of longest formatted string

TIME

DT_TIME

length of longest formatted string

TIMESTAMP

DT_TIMESTAMP

length of longest formatted string

LONG VARCHAR

DT_VARCHAR

32767

LONG BINARY

DT_BINARY

32767

Supplying a value 

The following table indicates how you specify lengths of values when you supply data to the database server in the SQLDA.

Only the data types shown in the table are allowed in this case. The DT_DATE, DT_TIME and DT_TIMESTAMP types are treated the same as DT_STRING when supplying information to the database; the value must be a NULL-terminated character string in an appropriate date format.

Embedded SQL Data Type

What the program must do to set the length when supplying data to the database

DT_STRING

length determined by terminating \0

DT_VARCHAR(n)

length taken from field in VARCHAR structure

DT_FIXCHAR(n)

length field in SQLDA determines length of string

DT_BINARY(n)

length taken from field in BINARY structure

DT_SMALLINT

No action required

DT_INT

No action required

DT_DECIMAL(p,s)

high byte of length field in SQLDA set to p, and low byte set to s

DT_FLOAT

No action required

DT_DOUBLE

No action required

DT_DATE

length determined by terminating \0

DT_TIME

length determined by terminating \0

DT_TIMESTAMP

length determined by terminating \0

DT_TIMESTAMP_STRUCT

No action required

DT_VARIABLE

length determined by terminating \0

Retrieving a value 

The following table indicates the values of the length field when you retrieve data from the database using a SQLDA. The sqllen field is never modified when you retrieve data.

Only the interface data types shown in the table are allowed in this case. The DT_DATE, DT_TIME and DT_TIMESTAMP data types are treated the same as DT_STRING when you retrieve information from the database. The value is formatted as a character string in the current date format.

Embedded SQL Data Type

What the program must set length field to when receiving

How the database returns length information after fetching a value

DT_STRING

length of buffer

\0 at end of string

DT_VARCHAR(n)

maximum length of VARCHAR structure (n+2)

len field of VARCHAR structure set to actual length

DT_FIXCHAR(n)

length of buffer

padded with blanks to length of buffer

DT_BINARY(n)

maximum length of BINARY structure (n+2)

len field of BINARY structure set to actual length

DT_SMALLINT

No action required

No action required

DT_INT

No action required

No action required

DT_DECIMAL(p,s)

high byte set to p and low byte set to s

No action required

DT_FLOAT

No action required

No action required

DT_DOUBLE

No action required

No action required

DT_DATE

length of buffer

\0 at end of string

DT_TIME

length of buffer

\0 at end of string

DT_TIMESTAMP

length of buffer

\0 at end of string

DT_TIMESTAMP_
STRUCT

No action required

No action required

Top of page  


Collection Contents Index Static and dynamic SQL Using stored procedures in Embedded SQL pdf/chap2.pdf