方法1:使用函數EM_GET_NUMBER_OF_ENTRIEShtml
這個函數使用起來很簡單,只須要將想查詢的數據庫表名稱維護進輸入參數IT_TABLES:sql
上圖說明這個函數支持批量操做,我查詢的兩張表名爲TADIR和PROGDIR.數據庫
執行函數,獲得表的條目數:app
方法2:使用ADBC函數
方法1的實現其實沒有什麼神奇之處,就是用Open SQL SELECT COUNT(*)來取得一張表的數據個數。工具
若是咱們使用的ABAP Netweaver底層數據庫用的是SAP HANA,那麼有一張元數據表M_TABLES,存放了全部表的元數據。spa
看個例子:code
下面一條SQL語句,能夠輕鬆得到表COMM_PRODUCT和TADIR的條目數:htm
SELECT * FROM M_TABLES WHERE TABLE_NAME IN ('COMM_PRODUCT', 'TADIR')blog
M_TABLES這張表的詳細信息能夠查看SAP幫助:https://help.sap.com/doc/4fe29514fd584807ac9f2a04f6754767/2.0.01/en-US/20c7689a75191014ad52c1beb40ce2d2.html
我寫了個簡單的ABAP工具,把對錶M_TABLES的操做封裝了起來:
class CL_CRM_HOME_TABLE_SIZE_TOOL definition public final create public . public section. TYPES: BEGIN OF ty_size, table_name TYPE char256, record_count TYPE int4, table_size TYPE int4, END OF ty_size. TYPES: tt_size TYPE TABLE OF ty_size with key table_name. class-methods GET_SIZE importing !IT_INPUT type STRING_TABLE returning value(RT_RESULT) type tt_size . protected section. private section. ENDCLASS. CLASS CL_CRM_HOME_TABLE_SIZE_TOOL IMPLEMENTATION. METHOD get_size. DATA(lv_in) = REDUCE string( INIT x TYPE string FOR <data> IN it_input NEXT x = SWITCH #( x WHEN space THEN |'{ <data> }'| ELSE x && ',' && |'{ <data> }'| ) ). TRY. DATA(lo_sql_con) = cl_sql_connection=>get_connection( ). DATA(lo_stmt) = lo_sql_con->create_statement( ). DATA: lv_stmt TYPE string. lv_stmt = |select table_name, record_count, table_size from m_tables where table_name in ({ lv_in })|. DATA(lo_res) = lo_stmt->execute_query( lv_stmt ). GET REFERENCE OF rt_result INTO DATA(lr_data). lo_res->set_param_table( lr_data ). lo_res->next_package( ). lo_res->close( ). CATCH cx_sql_exception INTO DATA(cx_root). WRITE:/ 'Error:', cx_root->get_text( ). RETURN. ENDTRY. ENDMETHOD. ENDCLASS.
上述class的消費方式:
REPORT zsize. DATA: lt_input TYPE String_table. lt_input = VALUE #( ( CONV string( 'TADIR' ) ) ( CONV string( 'TFDIR' ) ) ). DATA(lt_size) = cl_crm_home_table_size_tool=>get_size( lt_input ). cl_demo_output=>display_data( lt_size ).
執行上述report,顯示的結果以下:
本文提到的工具的源代碼請在個人SAP社區博客下載.
要獲取更多Jerry的原創文章,請關注公衆號"汪子熙":