EXEC SQL - Host Variables

Host variables are global or local variables declared in the ABAP program, or variables that are used in operand positions by native SQL statements. For identification purposes, the variable name has a colon (:) directly in front of it. Instead of specifying a variable itself, you can also specify a field symbol to which the variable is assigned. The specification of a dereferenced data reference variable is not possible.

You can use elementary fields and structures with elementary components as host variables. If a structure is listed in a native SQL statement after INTO, it is converted by the native SQL interface as if its components were listed as individual fields separated by commas.

In a SELECT statement, you can specify the SAP-specific addition STRUCTURE between INTO and an individual host variable. This addition has the effect that the host variable is treated like a structure, even if a non-typed formal parameter or a non-typed field symbol is specified. Otherwise, when several values are being passed, depending on the platform, either the first value only is passed or an exception occurs.

If you have assignments between host variales and fields in database tables, the native SQL interface passes a description of type, size, and storage location of the used ABAP fields to the database system. The actual database accesses and conversions are usually executed directly by the corresponding operations of the database system. In some cases, however, the native SQL interface executes extensive compatibility checks.

The conversion rules between ABAP data types and types of database columns are listed in the programming interface manuals of the appropriate database system - both for write accesses (INSERT, UPDATE) as well as for read (SELECT) accesses. These conversion rules apply also to the input and output parameters for database procedures. Any combinations listed there are undefined and should not be used.

Note

The indicator variables provided in the SAP standard, which can be specified after an operand to identify null values, can be specified in native SQL by a host variable that has to be of an external data type INT2.

Example

Reading a row from the database table SPFLI using native SQL and host variables. If a row was found, sy-subrc is set to 0; if not, it is set to 4. After INTO, the STRUCTURE addition could be specified. However, this is not necessary since wa can be statically recognized as a structure.

PARAMETERS: p_carrid TYPE spfli-carrid,
            p_connid TYPE spfli-connid.

DATA: BEGIN OF wa,
        cityfrom TYPE spfli-cityfrom,
        cityto   TYPE spfli-cityto,
      END OF wa.

EXEC SQL.
  SELECT cityfrom, cityto
         INTO :wa
         FROM spfli
         WHERE carrid = :p_carrid AND connid = :p_connid
ENDEXEC.