DBCO

实现SAP连接外部数据库

也可用SM30维护DBCON的内容

SAP提供了对原生sql的操作,主要有以下几个类组成:

  • CL_SQL_STATEMENT - Execution of SQL Statements
  • CL_SQL_PREPARED_STATEMENT - Prepared SQL Statements
  • CL_SQL_CONNECTION - Administration of Database Connections
  • CX_SQL_EXCEPTION - Exception Class for SQL Error

还会用到:

  • CL_SQL_RESULT_SET- Resulting Set of an SQL Query
  • CX_PARAMETER_INVALID- Superclass for Parameter Error

示例代码:

FUNCTION xxx.
TYPES: BEGIN OF t_sp_test,
a TYPE char10,
END OF t_sp_test,
t_sp_test_tab TYPE TABLE OF t_sp_test. DATA: lt_sp_test TYPE t_sp_test_tab,
ls_sp_test TYPE t_sp_test. DATA: lv_root_message TYPE string.
DATA: lv_sql_message TYPE string.
DATA: lv_param_message TYPE string.
DATA: lr_cxsql TYPE REF TO cx_sql_exception.
DATA: lr_cxpar TYPE REF TO cx_parameter_invalid.
DATA: l_con_ref TYPE REF TO cl_sql_connection.
DATA: l_stmt_ref TYPE REF TO cl_sql_statement.
DATA: l_res_ref TYPE REF TO cl_sql_result_set.
DATA: lv_stmt TYPE string.
DATA: l_dref TYPE REF TO data,
ncount TYPE i,
l_error TYPE c. result = 0. TRY.
CALL METHOD cl_sql_connection=>get_connection
EXPORTING
con_name = dbconn
RECEIVING
con_ref = l_con_ref.
CATCH cx_sql_exception INTO lr_cxsql.
result = 1.
lv_root_message = lr_cxsql->get_text( ).
lv_sql_message = lr_cxsql->sql_message.
CONCATENATE msg lv_root_message lv_sql_message INTO msg SEPARATED BY '/'.
EXIT.
ENDTRY. IF result <> 1.
LOOP AT sql INTO lv_stmt. CALL METHOD l_con_ref->create_statement
RECEIVING
stmt_ref = l_stmt_ref. TRY. CALL METHOD l_stmt_ref->execute_update
EXPORTING
statement = lv_stmt
RECEIVING
rows_processed = ncount. CATCH cx_sql_exception INTO lr_cxsql.
result = 1.
lv_root_message = lr_cxsql->get_text( ).
lv_sql_message = lr_cxsql->sql_message.
CONCATENATE msg lv_root_message lv_sql_message INTO msg SEPARATED BY '/'.
EXIT. CATCH cx_parameter_invalid INTO lr_cxpar.
result = 1.
lv_root_message = lr_cxpar->get_text( ).
lv_param_message = lr_cxpar->parameter.
CONCATENATE msg lv_root_message lv_sql_message INTO msg SEPARATED BY '/'.
EXIT.
ENDTRY.
ENDLOOP.
ENDIF. IF result = 1.
CALL METHOD l_con_ref->rollback.
ELSE.
CALL METHOD l_con_ref->commit.
ENDIF. GET REFERENCE OF ls_sp_test INTO l_dref. IF dbconn <> 'DEFAULT'.
CALL METHOD l_con_ref->close.
ENDIF.
ENDFUNCTION.
  • Import
    • DBCONN TYPE C
  • Export
    • RESULT TYPE I
    • MSG TYPE BAPI_MSG
  • Tables
    • SQL
上一篇:YAML的使用


下一篇:sprint1_11.15燃尽图(第二天)