Hace tiempo encontre esto en la WEB, Oajala te sriva.
Involved Classes
CL_SQL_CONNECTION – This class handles the external DB connection. If you want to connect to any other DB other than your default DB, you need to instantiate the connection object with connection name.
CL_SQL_STATEMENT – This class contains methods to execute the DB operations. When you instantiate an object for SQL, you can pass the Connection reference of type CL_SQL_CONNECTION. Use the Connection object if you want to execute the statement on other DB.
CL_SQL_PREPARED_STATEMENT – This class is inherited from CL_SQL_STATEMENT. This class will accept the SQL Statement which is ready to be understood by DB. Since DB doesn’t need to convert translate this to internal format, you can execute this multiple times and improve the performance over using CL_SQL_STATEMENT.
CL_SQL_RESULT_SET – Class would be used to get the result back from the SQL statements.
CX_SQL_EXCEPTION – Will raise this exception if any of the error occurs
Demo Program using CL_SQL_STATEMENT
A simple demo program to read the data from default database table T100 using the provided input.
REPORT znp_ADBC_demo.
*
CLASS lcl_main DEFINITION.
PUBLIC SECTION.
METHODS:
get_data,
generate_output.
PRIVATE SECTION.
DATA: t_output TYPE STANDARD TABLE OF t100.
ENDCLASS. "lcl_main DEFINITION
*
PARAMETERS: p_arbgb TYPE t100-arbgb OBLIGATORY DEFAULT '00'.
START-OF-SELECTION.
DATA: o_main TYPE REF TO lcl_main.
CREATE OBJECT o_main.
o_main->get_data( ).
o_main->generate_output( ).
*
CLASS lcl_main IMPLEMENTATION.
METHOD get_data.
DATA: lo_sql TYPE REF TO cl_sql_statement,
lo_result TYPE REF TO cl_sql_result_set,
lo_exc TYPE REF TO cx_sql_exception,
lt_cols TYPE adbc_column_tab,
lv_query TYPE string,
lo_output TYPE REF TO data,
dref2 TYPE REF TO data.
" Fill up the columns which you want to select from the DB
" The order should match with the output table
APPEND 'SPRSL' TO lt_cols.
APPEND 'ARBGB' TO lt_cols.
APPEND 'MSGNR' TO lt_cols.
APPEND 'TEXT' TO lt_cols.
CONCATENATE
`SELECT * `
`FROM T100 `
`WHERE sprsl = '` sy-langu `' `
`AND ARBGB = '` p_arbgb `' `
INTO lv_query.
TRY.
CREATE OBJECT lo_sql.
GET REFERENCE OF t_output INTO lo_output.
lo_result = lo_sql->execute_query( lv_query ).
lo_result->set_param_table( itab_ref = lo_output
corresponding_fields = lt_cols ).
IF lo_result->next_package( ) > 0.
EXIT.
ENDIF.
CATCH cx_sql_exception INTO lo_exc.
MESSAGE lo_exc TYPE 'I' DISPLAY LIKE 'E'.
ENDTRY.
ENDMETHOD. "get_Data
METHOD generate_output.
DATA: o_salv TYPE REF TO cl_salv_table.
cl_salv_table=>factory(
IMPORTING
r_salv_table = o_salv
CHANGING
t_table = t_output ).
o_salv->display( ).
ENDMETHOD. "generate_otuput
ENDCLASS. "lcl_main IMPLEMENTATION
Variations
You can also provide field = ? in the Query and use the method SET_PARAM to set the proper data reference to the field in question.
CONCATENATE
`SELECT * `
`FROM T100 `
`WHERE sprsl = '` sy-langu `' `
`AND ARBGB = ?`
INTO lv_query.
* After SQL Instance & Before Executing the statement
DATA: lo_arbgb TYPE REF TO data.
GET REFERENCE OF p_arbgb INTO lo_arbgb.
lo_sql->set_param( lo_arbgb ).