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 

Writer Stream, Fill Database Table

The example demonstrates how data can be written to a database table using a writer stream.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA wa TYPE demo_blob_table WRITER FOR COLUMNS picture.

    cl_demo_input=>request( CHANGING field = demo=>name ).

    TRY.
        wa-name = name.
        INSERT demo_blob_table FROM @wa.
        IF sy-subrc = 4.
          DATA(subrc) = sy-subrc.
        ELSE.

          DATA(stmnt) = wa-picture->get_statement_handle( ).
          LOOP AT pict INTO DATA(xline).
            wa-picture->write( CONV #( xline ) ).
          ENDLOOP.
          wa-picture->close( ).

          IF stmnt->get_db_count( ) <> 1.
            subrc = 4.
          ENDIF.

        ENDIF.
      CATCH cx_stream_io_exception cx_close_resource_error.
        subrc = 4.
    ENDTRY.

    IF subrc = 0.
      cl_demo_output=>display(
        'One line inserted, you can run DEMO_DB_READER now' ).
    ELSE.
      cl_demo_output=>display(
        'Error during insertion' ).
    ENDIF.

Description

The method main derives an LOB handle structure from the structure of the database table DEMO_BLOB_TABLE, where the component PICTURE is declared as an LOB handle component for a binary writer stream.

This writer stream is created in the statement INSERT and then the content of the internal table pict is passed to it sequentially. After the writer stream is closed, the content of the internal table is stored in the database table as a binary string.

Here, the content of the internal table is a figure in GIF format, which is retrieved from the Mime Repository using an auxiliary method. The address there is also used as a database table key. Any existing rows are deleted in the static constructor so that the example always works.

The number of changed rows is checked by an object of the class CL_ABAP_SQL_CHANGING_STMNT.

The content can be read again using the executable reader stream example.