Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing External Data →  ABAP Database Access →  ABAP SQL →  ABAP SQL - Streaming and Locators →  ABAP SQL - Examples of LOB Handles 

Locator, Access to Column Content

This example demonstrates how to access texts in database tables using locators.

Other versions: 7.31 | 7.40 | 7.54

Source Code

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

    DATA: pattern  TYPE string VALUE 'ABAP',
          language TYPE sy-langu.

    language = sy-langu.
    cl_demo_input=>add_field( CHANGING field = pattern ).
    cl_demo_input=>request(   CHANGING field = language ).

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

          length += otr_text_locator->get_length( ).

          IF otr_text_locator->find( start_offset = 0
                                    pattern      = pattern ) <> -1.
            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.

    cl_demo_output=>display(
      |Average length of strings in database table: { avg }\n\n| &&
      |Occurrences of "{ pattern WIDTH = strlen( pattern ) }" | &&
      |in strings of database table: { hits }| ).

Description

In this example, a SELECT loop and locators created there are used to access the text column of a database table in which texts from Online Text Repository (OTR) are stored in a column of the type STRING.

The methods of the locators make it possible to get information about the texts without them having to be transported into the ABAP program. In this 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.