INFORMIX
Informix-ESQL/C Programmer's Manual
Chapter 1: Programming with INFORMIX-ESQL/C
Home Contents Index Master Index New Book

Declaring and Using Host Variables in SQL Statements

In an ESQL/C application that uses embedded SQL statements, the SQL statements can refer to the contents of host variables. A host variable is a C program variable that you use in an embedded SQL statement. Informix uses the term host variable because you declare the variable in the host program, the C program. The SQL statements are guests in the host program. They use host variables to transfer information between the C host program and the database.

You can use host variables in ESQL/C expressions in the same way that you use literal values. To use a host variable in an SQL statement, you must follow these steps in your ESQL/C program:

1. Declare the host variable within the C program.

    2. Assign a value to the host variable.

    3. Specify the host variable in an embedded SQL statement.

Declaring a Host Variable

You must define the data storage that a host variable needs before you can use that variable in an ESQL/C program. To assign an identifier to the variable and associate it with a data type, you declare the variable.

You declare host variables within the ESQL/C program as ordinary C variables, with the same basic syntax as C variables. To identify the variable as a host variable, you must declare it in either of the following ways:

    Make sure you terminate the statements EXEC SQL BEGIN DECLARE SECTION and EXEC SQL END DECLARE SECTION with semicolons.

Important: Using the EXEC SQL BEGIN DECLARE SECTION and EXEC SQL END DECLARE SECTION keywords conforms to ANSI standards.
Within the declaration itself, you must specify the following information:

For examples of the EXEC SQL and dollar sign ($) formats for host-variable declarations, see "Sample Host-Variable Declarations".

Host-Variable Names

The name of the host variable must conform to the naming conventions of the C language. In addition, you must follow any limitations that your C compiler imposes. In general, a C variable must begin with a letter or an underscore (_) and can include letters and digits. C variable names are case sensitive, so the variables hostvar and HostVar are distinct. (For more information, see "Handling Case Sensitivity in Embedded SQL Statements".)

GLS
You can use non-ASCII (non-English) characters in ESQL/C host-variable names if your client locale supports these non-ASCII characters. For more information on how the client locale affects host-variable names, see the
Informix Guide to GLS Functionality.

Tip: Good programming practice requires that you create a naming convention for host-variable names.

Host-Variable Data Types

Because a host variable is a standard C variable, you must assign a C data type to the variable when you declare it. Likewise, when you use a host variable in an SQL statement, you also associate it with an SQL data type.

For more information about the relationship between SQL data types and C data types, refer to Chapter 2, "INFORMIX-ESQL/C Data Types." In addition, the Informix Guide to SQL: Reference contains information on the SQL data types.

You can also declare host variables as many of the more complex C data types, such as pointers, structures, typedef expressions, and function parameters. For more information, see "Using Host Variables in Data Structures".

Initial Host-Variable Values

ESQL/C allows you to declare host variables with normal C initializer expressions. However, initializers with character strings cannot contain embedded semicolons or ESQL/C keywords. Some valid examples of C initializers follow:

The ESQL/C preprocessor does not check initializer expressions for valid C syntax; it simply copies them to the C source file. The C compiler diagnoses any errors.

Scope of Host Variables

The scope of reference, or simply the scope, of a host variable is that portion of the program in which the host variable can be accessed. The placement of the ESQL/C declaration statement determines the scope of the variable as follows:

    Only statements within that program block can access the variable.

    All program blocks that occur after the declaration can access the variable.

Host variables that you declare within a block of code are local to that block. You define a block of code with a pair of braces ({ }).

For example, the host variable blk_int in Figure 1-8 is valid only in the block of code between the braces, whereas p_int is valid both inside and outside the block.

Figure 1-8
Declaring Host Variables Inside and Outside a Code Block

You can nest blocks up to 16 levels. The global level counts as level one.

The following C rules that govern regular C variables govern the scope of ESQL/C host variables as well:

Sample Host-Variable Declarations

Figure 1-9 shows an example of how to use the EXEC SQL syntax to declare host variables.

Figure 1-9
Declaring Host Variables with the EXEC SQL Syntax

Figure 1-10 shows an example of how to use the dollar sign ($) notation to declare host variables.

Figure 1-10
Declaring Host Variables with the Dollar Sign ($) Notation

For information on how to use a host variable in an SQL statement, see page 1-21.

Assigning a Value to a Host Variable

You can use host variables to contain the following types of information:

Host variables can appear within an SQL statement as syntax allows. (For information about the syntax of SQL statements, see the Informix Guide to SQL: Syntax.) However, you must precede the host-variable name with a symbol to distinguish it from regular C variables. For more information, see "Specifying Host Variables".

SQL Identifiers

An SQL identifier is the name of a database object. The following objects are examples of SQL identifiers:

As syntax allows, you can use a host variable within an embedded SQL statement to hold the name of an SQL identifier.

The identifier name can consist of letters, numbers, and underscores (_). The first character must be a letter. In most contexts, an identifier can have from 1 to 18 characters. (For more information on naming conventions for SQL identifiers, see the Identifier segment in the Informix Guide to SQL: Syntax.)

If an identifier name does not conform to these naming conventions, you must use a delimited identifier. A delimited identifier is an SQL identifier that is enclosed in double quotes ('' ").

Important: When you use double quotes (" ") to delimit identifiers, you conform to ANSI standards; single quotes (' ') delimit strings.
Use delimited identifiers when your program must specify some identifier name that would otherwise be syntactically invalid. Examples of possible invalid identifiers include:

To use delimited identifiers, you must compile and run your ESQL/C program with the DELIMIDENT environment variable set. You can set DELIMIDENT at either of the following phases:

Important: When you use the DELIMIDENT environment variable, you can no longer use double quotes (" ") to delimit strings. If you want to indicate a quoted string, enclose the text with single quotes (' ').
Delimited identifiers are case sensitive. All database object names that you place within quotes maintain their case. Keep in mind that ESQL/C restricts identifier names to a maximum of 18 characters.

Figure 1-11 shows a delimited identifier that specifies nonalphabetic characters in both a cursor name and a statement ID.

Figure 1-11
Using Delimited Identifiers for a Cursor Name

In Figure 1-11, you could also list the cursor name or statement ID directly in the SQL statement. For example, the following PREPARE statement is also valid (with DELIMIDENT set):

If you set DELIMIDENT, the SELECT string in the preceding PREPARE statement must be enclosed in single quotes for the preprocessor to treat it as a string. If you enclose the statement in double quotes, the preprocessor treats it as an identifier.

To declare a cursor name that contains a double quote, you must use escape characters in the delimited identifier string. For example, to use the string "abc" as a cursor name, you must escape the initial quote in the cursor name:

In the preceding example, the cursor name requires several escape characters:

    Without the escape character, the C compiler would interpret the double quote as the end of the string.

    The first escapes the double quote and the second is the literal double quote. The ANSI standard states that you cannot use a backslash to escape quotes. Instead, you must escape the quote in the cursor name with another quote.

Figure 1-12 shows the string that contains the cursor name as it is processed.

Figure 1-12
Escaped Cursor Name String As It Is Processed
Processor After Processing

ESQL/C preprocessor

\"\"abc

C Compiler

""abc

ANSI-compliant database server

"abc

The following restrictions apply to delimited identifiers:

    This restriction prevents the creation of a database name like " " and avoids conflict with the syntax for INFORMIX-SE database names.

    The DELIMIDENT environment variable applies only to database identifiers.

Null Values in Host Variables

A null value represents unknown or not applicable values. This value is distinct from all legal values in any given data type. The representation of null values depends on both the computer and the data type. Often, the representation does not correspond to a legal value for the C data type. Do not attempt to perform arithmetic or other operations on a host variable that contains a null value.

A program must, therefore, have some way to recognize a null value. To handle null values, ESQL/C provides the following features:

Using Host Variables in Data Structures

ESQL/C supports the use of host variables in the following data structures:

Arrays of Host Variables

ESQL/C supports the declaration of arrays of host variables. You must provide an integer value as the size of the array when you declare the array. An array of host variables can be either one or two dimensional.

You can use elements of an array within ESQL/C statements. For example, if you provide the following declaration:

you can use the following syntax:

You can also use the array name alone within some SQL statements if the array is of type CHAR. For information on specific statements, consult the Informix Guide to SQL: Syntax.

C Structures as Host Variables

ESQL/C supports the declaration of a C structure (struct) as a host variable. You can use the components of the structure within ESQL/C statements.

The following definition of the cust_rec variable serves as a host variable for the first three columns of the customer table in the stores7 database:

The following INSERT statement specifies the components of the cust_rec host variable in its VALUES clause:

If an SQL statement requires a single host variable, you must use the structure component name to specify the host variable. Informix requires structure component names in the SET clause of an UPDATE statement.

In SQL statements that allow a list of host variables, you can specify the name of the C structure and ESQL/C expands the name of the structure variable to each of its component elements. You can use this syntax with SQL statements such as the FETCH statement with an INTO clause or the INSERT statement with a VALUES clause.

The following INSERT statement specifies the entire cust_rec structure in its VALUES clause:

This insert performs the same task as the insert that specifies the individual component names of the cust_rec structure.

C typedef Expressions as Host Variables

ESQL/C supports standard C typedef expressions and allows their use as host variables. For example, the following code creates the smallint type as a short integer and the serial type as a long integer. It then declares a row_nums variable as an array of serial variables and a variable counter as a smallint.

You cannot use a typedef expression that names a multidimensional array or a union as a host variable.

Pointers as Host Variables

You can use a pointer as a host variable as long as your program uses the pointer to input data to an SQL statement. For example, Figure 1-13 shows how you can associate a cursor with a statement and insert values into a table.

Figure 1-13
Declaring a Character Pointer to Input Data

Figure 1-14 shows how to use an integer pointer to input data to an INSERT statement.

Figure 1-14
Declaring an Integer Pointer to Input Data

If you use a pointer that is a host variable to receive data from a SELECT statement, you receive a compile-time warning and your results might be truncated.

Function Parameters as Host Variables

You can use host variables as parameters to functions. You must precede the name of the host variable with the parameter keyword to declare it as a function parameter. For example, Figure 1-15 shows a code fragment with Kernighan & Ritchie-style prototype declaration that expects three parameters, two of which are host variables.

Figure 1-15
Using EXEC SQL to Declare Host Variables as Parameters to a Kernighan & Ritchie-Style Function Declaration

You can also declare parameter host variables with the dollar sign ($) notation. For example, Figure 1-16 shows the function header in Figure 1-15, with the dollar sign ($) notation.

Figure 1-16
Using $ to Declare Host Variables as Parameters to a Function

You can declare parameters in an ANSI-style prototype function declaration as host variables as well. It is also possible to put all parameters to a prototype function declaration inside the EXEC SQL declare section, even if some of the parameters cannot be used as host variables. Figure 1-17 shows that the function pointer f can be included in the EXEC SQL declare section, even though it is not a valid host variable type and cannot be used as a host variable.

Figure 1-17
Using EXEC SQL to Declare Host Variables as Parameters to ANSI-Style Function Declaration

The functionality that allows inclusion of function parameters inside of the EXEC SQL declare section is in compliance with the requirement that any valid C declaration syntax must be allowed inside of the EXEC SQL declare sections to use common header files for C and ESQL/C source files. For more information on how to use common header files between C and ESQL/C source files, see "Running the C Preprocessor Before the ESQL/C Preprocessor".

Important: If you want to define ESQL/C host variables which are ANSI-style parameters, you must use the EXEC SQL BEGIN DECLARE SECTION and the EXEC SQL END DECLARE SECTION syntax. You cannot use the $BEGIN DECLARE and $END DECLARE syntax. This is because SQL statements that begin with the dollar sign ($) notion must end with a semicolon (;). However, ANSI syntax requires that each parameter in a parameter list should not end with a semicolon terminator, but with a comma (,) delimiter.
The following limitations apply to using host variables as function parameters:

Using Indicator Variables

When an SQL statement returns a value, it puts this value in the associated host variable. In some situations, you can use an indicator variable to provide additional information about the value that is returned. An indicator variable is an ESQL/C variable that is associated with a host variable. When you pair a host variable and an indicator variable in this way, Informix calls the host variable the main variable. When ESQL/C returns a value into the main variable, it can also set the indicator variable so that your program can determine more about the value.

The indicator variable provides additional information in the following situations:

This section describes how your program can use indicator variables to determine additional information about a returned value. Specifically, it contains sections about the following topics:

Values in Indicator Variables

ESQL/C can set an indicator variable in the following two cases:

Indicating Null Values
When an ESQL/C statement returns a null value to a host variable (through the INTO clause of a SELECT or FETCH statement), the actual value in the main variable might not be a meaningful C value. Your program can take one of the following actions:

    Your program can check for this value to determine whether a value is null.

If the return code is not null, ESQL/C sets the indicator variable to 0.

The NULL keyword of an INSERT statement allows you to insert a null value into a table row. As an alternative to the NULL keyword in an INSERT statement, you can use a negative indicator variable with the host variable.

When you return aggregate function values into a host variable, keep in mind that when the database server performs an aggregate function on an empty table, the result of the aggregate operation is the null value. The only exception to this rule is the COUNT(*) aggregate function, which in this case returns a zero (0).

Important: If you use the DATASKIP feature of the database server and you turn this feature on, an aggregate function might also return null if all the fragments that are on-line are empty or if all fragments are off-line. When DATASKIP is off, the values of the indicator variables (or the risnull() function) are not valid if the operation is not successful; therefore, check the SQLCODE value after the operation and before you check for null values.
SE
The DATASKIP feature is not available for SE.

Indicating Truncated Values
When an SQL statement returns a non-null value into a host-variable character array, it might truncate the value to fit into the variable. If you define an indicator variable for this host variable, ESQL/C:

If you do not define an indicator variable, ESQL/C still sets SQLSTATE and sqlca.sqlwarn to signal the truncation. However, your program has no way to determine how much data has been truncated.

If the database server returns a value that is neither truncated nor null, ESQL/C sets the indicator variable to 0.

Declaring Indicator Variables

You declare indicator variables in the same way as host variables. You put the declaration within an ESQL/C block, as shown in the following example:

For more information, see "Declaring a Host Variable".

Indicator variables can be of any valid host-variable data type except DATETIME or INTERVAL. Usually, you declare an indicator variable as an integer. For example, suppose your program declares a host variable that is called name. You could declare a short integer-indicator variable that is called nameind, as the following example shows:

GLS
You can use non-ASCII (non-English) characters in ESQL/C indicator-variable names if your client locale supports these non-ASCII characters. For more information on how the client locale affects host-variable names, see the
Informix Guide to GLS Functionality.

Associating an Indicator Variable with a Host Variable

You associate a host variable and its indicator variable when you list them in an SQL statement. You can associate these variables in one of two ways:

Important: The "INDICATOR :hostvarind" format conforms to ANSI standards.
    You can use a dollar sign ($) instead of a colon (:), but the colon makes the code easier to read.

$hostvar:hostvarind
$hostvar :hostvarind

An Example Using Indicator Variables

The code segments in Figure 1-18 and Figure 1-19 show examples of how to use indicator variables with host variables. Both examples use indicator variables to perform the following tasks:

    If you define lname in the customer table with a length that is longer than 15 characters, nameind contains the actual length of the lname column. The name host variable contains the first 15 characters of the lname value. (The string name must be terminated with a null character.) If the last name of the company representative with customer_num = 105 is shorter than 15 characters, ESQL/C truncates only the trailing blanks.

    If company has a null value for this same customer, compind has a negative value. The contents of the character array comp cannot be predicted.

Figure 1-18 shows an ESQL/C program that uses the EXEC SQL syntax for the SQL statements.

Figure 1-18
Using Indicator Variables with
EXEC SQL and
the Colon (:) Symbol

Figure 1-18 uses the INDICATOR keyword to associate the main and indicator variables. This method complies with the ANSI standard.

Figure 1-19 shows an ESQL/C program that uses the dollar sign ($) format for the SQL statements.

Figure 1-19
Using Indicator Variables with
the Dollar Sign ($) Notation




Informix-ESQL/C Programmer's Manual, version 9.1
Copyright © 1998, Informix Software, Inc. All rights reserved.