Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing External Data →  ABAP - Database Accesses →  Open SQL →  Open SQL - Streaming and Locators →  Open SQL - Examples forLOB Handles 

Locator, Access to Column Content

The example demonstrates how to access texts in the database by using locators.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA: otr_text_locator TYPE REF TO cl_abap_db_c_locator,
          search_pattern   TYPE string,
          length           TYPE i,
          hits             TYPE i,
          avg              TYPE p DECIMALS 2,
          msg              TYPE string.

    search_pattern = pattern.

    TRY.
        SELECT text
               FROM sotr_textu
               INTO otr_text_locator
               WHERE langu = language.

          length = length + otr_text_locator->get_length( ).

          IF otr_text_locator->find( start_offset = 0
                                    pattern = search_pattern ) <> -1.
            hits = hits + 1.
          ENDIF.

          otr_text_locator->close( ).
        ENDSELECT.
      CATCH cx_lob_sql_error.
        WRITE 'Exception in locator' COLOR = 6.
        RETURN.
    ENDTRY.

    avg = length / sy-dbcnt.

    msg = |Average length of strings in database table: { avg }|.
    WRITE: / msg.
    msg = |Occurrences of "{ pattern WIDTH = strlen( pattern ) }" | &&
          |in strings of database table: { hits }|.
    WRITE: / msg.

Description

In this example, the text column of a database table in which texts from the Online Text Repository (OTR) are stored in a column of the type STRING, is accessed in a SELECT loop using locators which were created there.

The methods of the locators allow you to get information about the texts without them having to be transported into the ABAP program. In the example, the average length of all texts of a language is determined and the number of texts which contain a specific character string is returned.

Since the number of locators in each database LUW is limited, the locator created there must be closed explicitly at the end of each SELECT loop. The runtime error DBIF_RSQL_TOO_MANY_LOB_HANDLES occurs if the maximum number of open locators is exceeded.