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,
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 = length + otr_text_locator->get_length( ).
IF otr_text_locator->find( start_offset = 0
pattern = 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.
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, 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.