介绍两个SAP函数FREE_SELECTIONS_DIALOG和FREE_SELECTIONS_INIT,通过这两个函数能生成基于某个数据库表的动态选择屏幕。
比如要根据销售订单抬头表VBAK生成动态屏幕,
对应的完整代码:
REPORT ztest_selection_dyn. DATA: lv_selection_id TYPE rsdynsel-selid, lt_tables_tab TYPE STANDARD TABLE OF rsdstabs, ls_tables_tab TYPE rsdstabs. DATA: lt_fields_tab TYPE STANDARD TABLE OF rsdsfields, lt_where_clauses TYPE rsds_twhere. ls_tables_tab-prim_tab = 'VBAK'. "数据库表名 APPEND ls_tables_tab TO lt_tables_tab. CALL FUNCTION 'FREE_SELECTIONS_INIT' EXPORTING kind = 'T' IMPORTING selection_id = lv_selection_id TABLES tables_tab = lt_tables_tab EXCEPTIONS fields_incomplete = 1 fields_no_join = 2 field_not_found = 3 no_tables = 4 table_not_found = 5 expression_not_supported = 6 incorrect_expression = 7 illegal_kind = 8 area_not_found = 9 inconsistent_area = 10 kind_f_no_fields_left = 11 kind_f_no_fields = 12 too_many_fields = 13 dup_field = 14 field_no_type = 15 field_ill_type = 16 dup_event_field = 17 node_not_in_ldb = 18 area_no_field = 19 OTHERS = 20. IF sy-subrc EQ 0. CALL FUNCTION 'FREE_SELECTIONS_DIALOG' EXPORTING selection_id = lv_selection_id title = '选择' frame_text = '查询条件' as_window = '' "不显示成窗口 IMPORTING where_clauses = lt_where_clauses "返回选择条件 TABLES fields_tab = lt_fields_tab "选择画面中选中字段 EXCEPTIONS internal_error = 1 no_action = 2 selid_not_found = 3 illegal_status = 4 OTHERS = 5. IF sy-subrc EQ 0. ENDIF. ENDIF.
运行结果:
然后可以按需要将左侧的vbak中的字段,选到右边生成选择屏幕。
以上。