SAP NetWeaver AS ABAP Release 740, ©Copyright 2014 SAP AG. All rights reserved.
ABAP Keyword Documentation → ABAP − Reference → Processing External Data → ABAP Database Accesses → Open SQL → Open SQL - Streaming and Locators → Open SQL - Examples forLOB Handles →Write Stream, Fill Database
The example demonstrates how data can be written to a database table using a write stream.
Source Code
REPORT demo_db_writer.
CLASS demo DEFINITION.
PUBLIC SECTION.
CLASS-METHODS: class_constructor,
main.
PRIVATE SECTION.
TYPES:
pict_line(1022) TYPE x,
pict_tab TYPE STANDARD TABLE OF pict_line .
CLASS-DATA:
name TYPE c LENGTH 255
VALUE '/SAP/PUBLIC/BC/ABAP/Sources/ABAP_Docu_Logo.gif',
pict TYPE pict_tab.
CLASS-METHODS get_pict_tab
IMPORTING
mime_url TYPE csequence
EXPORTING
pict_tab TYPE STANDARD TABLE.
ENDCLASS.
CLASS demo IMPLEMENTATION.
METHOD main.
DATA: xline TYPE xstring,
wa TYPE demo_blob_table WRITER FOR COLUMNS picture,
stmnt TYPE REF TO cl_abap_sql_changing_stmnt,
subrc TYPE sy-subrc.
cl_demo_input=>request( CHANGING field = demo=>name ).
TRY.
wa-name = name.
INSERT demo_blob_table FROM wa.
IF sy-subrc = 4.
subrc = 4.
ELSE.
stmnt = wa-picture->get_statement_handle( ).
LOOP AT pict INTO xline.
wa-picture->write( 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.
ENDMETHOD.
METHOD class_constructor.
get_pict_tab(
EXPORTING
mime_url = name
IMPORTING
pict_tab = pict ).
DELETE FROM demo_blob_table WHERE name = name.
ENDMETHOD.
METHOD get_pict_tab.
DATA pict_wa TYPE xstring.
DATA length TYPE i.
DATA mime_api TYPE REF TO if_mr_api.
mime_api = cl_mime_repository_api=>get_api( ).
mime_api->get( EXPORTING i_url = mime_url
IMPORTING e_content = pict_wa
EXCEPTIONS OTHERS = 4 ).
IF sy-subrc = 4.
RETURN.
ENDIF.
CLEAR pict_tab.
length = xstrlen( pict_wa ).
WHILE length >= 1022.
APPEND pict_wa(1022) TO pict_tab.
SHIFT pict_wa BY 1022 PLACES LEFT IN BYTE MODE.
length = xstrlen( pict_wa ).
ENDWHILE.
IF length > 0.
APPEND pict_wa TO pict_tab.
ENDIF.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
demo=>main( ).
Description
In the main method, a LOB handle structure is derived from the structure of the DEMO_BLOB_TABLE database table , whereby the PICTURE component is declared as a LOB handle component for a binary write stream.
This write stream is created in the INSERT statement and then sequentially receives the content of the pict internal table. After closing the write stream, 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. An existing row is deleted in the static constructor so that the example always works.
The number of changed rows is checked by an object of the CL_ABAP_SQL_CHANGING_STMNT class.
The content can be read again using the example for read streams.