项目中使用的接口取数采用的是DBLink的方式,对方提供的表名太长,超过标准程序ADBC_QUERY的参数长度,于是写了一份简单的测试代码用来测试连接和取数。
DBCO配置:
测试程序:
程序源码:
*&---------------------------------------------------------------------*
*& Report ZTEST_ADBC_QUERY
*&
*&---------------------------------------------------------------------*
*&
*& 测试DBLink数据读取
*&---------------------------------------------------------------------*REPORT ztest_adbc_query.DATA:lo_adbc_connect TYPE REF TO cl_sql_connection,lo_adbc_query TYPE REF TO cl_sql_statement,lo_adbc_result TYPE REF TO cl_sql_result_set,lo_metadata TYPE REF TO data,lt_metadata TYPE adbc_rs_metadata_descr_tab.DATA:lo_sql_exception TYPE REF TO cx_sql_exception,lo_parameter_invalid TYPE REF TO cx_parameter_invalid,lo_parameter_invalid_range TYPE REF TO cx_parameter_invalid_range,lo_parameter_invalid_type TYPE REF TO cx_parameter_invalid_type,lo_struct_creation TYPE REF TO cx_sy_struct_creation,lo_table_creation TYPE REF TO cx_sy_table_creation,lo_exception TYPE REF TO data,lv_err_text TYPE string.DATA:lo_structdescr TYPE REF TO cl_abap_structdescr,lo_tabletype TYPE REF TO cl_abap_tabledescr,lo_result_ref TYPE REF TO data.FIELD-SYMBOLS:<lt_itab> TYPE STANDARD TABLE.PARAMETERS:p_conn TYPE dbcon-con_name,p_sql TYPE string,p_upto TYPE i DEFAULT 10.START-OF-SELECTION.
* 获取连接TRY .lo_adbc_connect = cl_sql_connection=>get_connection( con_name = p_conn ).CATCH cx_sql_exception INTO lo_sql_exception.lv_err_text = lo_sql_exception->get_text( ).WRITE lv_err_text.RETURN.ENDTRY.* 创建连接对象lo_adbc_query = lo_adbc_connect->create_statement( ).* 执行SqlTRY .lo_adbc_result = lo_adbc_query->execute_query( p_sql ).CATCH cx_sql_exception INTO lo_sql_exception.lv_err_text = lo_sql_exception->get_text( ).WRITE lv_err_text.RETURN.CATCH cx_parameter_invalid INTO lo_parameter_invalid.lv_err_text = lo_parameter_invalid->get_text( ).WRITE lv_err_text.RETURN.ENDTRY.* 获取元数据TRY .lt_metadata = lo_adbc_result->get_metadata( ).CATCH cx_sql_exception.lv_err_text = lo_sql_exception->get_text( ).WRITE lv_err_text.RETURN.ENDTRY.* 获取元数据字段信息TRY .lo_metadata = lo_adbc_result->get_struct_ref( md_tab = lt_metadatap_strict = abap_false ).CATCH cx_sy_struct_creation INTO lo_struct_creation.lv_err_text = lo_struct_creation->get_text( ).WRITE lv_err_text.RETURN.CATCH cx_parameter_invalid_range INTO lo_parameter_invalid_range.lv_err_text = lo_parameter_invalid_range->get_text( ).WRITE lv_err_text.RETURN.ENDTRY.* 创建结构类型对象lo_structdescr ?= cl_abap_typedescr=>describe_by_data_ref( lo_metadata ).* 创建表类型对象TRY .lo_tabletype = cl_abap_tabledescr=>create( p_line_type = lo_structdescrp_table_kind = cl_abap_tabledescr=>tablekind_std ).CATCH cx_sy_table_creation INTO lo_table_creation.lv_err_text = lo_table_creation->get_text( ).WRITE lv_err_text.RETURN.ENDTRY.* 创建表对象引用CREATE DATA lo_result_ref TYPE HANDLE lo_tabletype.* 传递sql结果至表对象引用TRY .lo_adbc_result->set_param_table( itab_ref = lo_result_ref ).CATCH cx_parameter_invalid INTO lo_parameter_invalid.lv_err_text = lo_parameter_invalid->get_text( ).WRITE lv_err_text.RETURN.ENDTRY.* 获取所有数据TRY .lo_adbc_result->next_package( EXPORTING upto = p_upto ).CATCH cx_sql_exception INTO lo_sql_exception.lv_err_text = lo_sql_exception->get_text( ).WRITE lv_err_text.RETURN.CATCH cx_parameter_invalid_type INTO lo_parameter_invalid_type.lv_err_text = lo_parameter_invalid_type->get_text( ).WRITE lv_err_text.RETURN.ENDTRY.* 关闭连接lo_adbc_result->close( ).* 赋值到动态内表ASSIGN lo_result_ref->* TO <lt_itab>.cl_demo_output=>display( <lt_itab> ).