sapdev logo background
sapdev logo sapdev logo
Comments

ABAP EXEC Statement syntax, information and example SAP source code



Return to Statement index



EXEC SQL

Short Reference

ABAP Syntax EXEC SQL.
...
ENDEXEC.

What does it do? These statements define an area in an ABAP program in which one or more Native SQL statements can be specified statically. The area between EXEC and
ENDEXEC is not checked completely by the syntax check. The statements entered there are passed to the Native SQL interface and processed there as follows:
Almost all SQL statements that are valid for the addressed database system can be included between EXEC and ENDEXEC , in particular the DDL statements. These SQL statements are passed from the Native SQL interface to the database system largely unchanged. The syntax rules are specified by the database system, in particular the case sensitivity rules for database objects. If the syntax allows a separator character between individual statements, multiple Native SQL statements can be included between EXEC and ENDEXEC . Generally, the semicolon ( ; ) is used as the separator character.

SAP-specific Native-SQL language elements can also be included between EXEC and ENDEXEC . These statements are not passed directly from the Native SQL interface to the database, but are converted appropriately. These SAP-specific language elements are:
  • Literals

  • Host variables

  • Statements for cursor processing

  • Database procedure calls

  • Statements for establishing database connections

  • All Native SQL statements bypass SAP buffering. Automatic client handling is not performed.

    System Fields
    The statement ENDEXEC sets the system fields sy-subrc and sy-dbcnt . When using the obsolete addition PERFORMING , note that implicit cursor processing is carried out and the system fields are set for every read.
    sy-subrc Meaning
    0The statements between EXEC and ENDEXEC were executed successfully.
    4The statements between EXEC and ENDEXEC were not successful. After implicit cursor processing with PERFORMING , sy-subrc always contains the value 4.

    The ENDEXEC statement sets sy-dbcnt to the number of table rows processed in the last Native SQL statement. After implicit cursor processing with PERFORMING , sy-dbcnt contains the total number of rows read. If an overflow occurs because the number or rows is greater than 2,147,483,647, sy-dbcnt is set to -1.

    Latest notes: Programs with Native SQL statements are generally dependent on the database system used, so that they cannot be executed in all AS ABAP systems. This is especially true for the examples in this section, which were written for Informix database systems, unless otherwise stated.

    If insertions or modifications with Native SQL statements
    INSERT or UPDATE would cause double rows with regard to the primary table key, no exception is raised. Instead, sy-subrc is set to 4. However, if another operation, such as executing a Stored Procedure , would cause a double row, an exception would be raised.

    The client ID of a database table must be specified explicitly. Remember that application programs should only use the data in the current client. In
    megatenancy systems, this is checked by the ABAP runtime environment. (See also Cross-Client Database Access ).

    The obsolete addition PERFORMING (not allowed in classes) executes implicit cursor processing and must no longer be used. The obsolete statement EXIT FROM SQL can be used to exit this type of processing.

    Native SQL statements used for transaction control ( COMMIT
    , ROLLBACK ) are detected by the database interface and the actions required at the end of a transaction are performed.

    The static embedding of Native SQL statements between EXEC SQL and ENDEXEC is replaced by dynamic passes to objects from ADBC classes. New features in the Native SQL
    in interface are now developed only in ADBC . Only ADBC
    should be used in new programs.

    Example ABAP Coding The following example demonstrates how a simple embedded
    Native SQL statement can be replaced by ADBC
    . The use of the instance operator NEW removes the need for a helper variable of type
    CL_SQL_STATEMENT when creating objects.
    "Static Native SQL
    EXEC SQL.
    COMMIT WORK
    ENDEXEC.

    "Dynamic Native SQL
    NEW cl_sql_statement( )->execute_update( `COMMIT WORK` ).

    Example ABAP Coding See the executable example for static Native SQL .
    Documentation extract taken from SAP system, � Copyright SAP AG. All rights reserved




    EVENTS_PARAMETERS
    EXEC_CONNECTION




    comments powered by Disqus