需求是想做一个维护查询条件的配置就,就是用户可以配置多个查询 条件组合,然后程序运行中,依据数据或者其它的配置判断 使用那一个条件组合,如图,用户在配置时设置各种条件组合,最终生成SELECT 的WHERE条件,把SELECT的条件保存到表中,再次选择此条件组合时,也可以使用此WHERE条件打开条件组合配置窗口。
以下是生成WHERE条件的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
FUNCTION ZFM_ZTRE03017_CHANGE_GZ_WHERE. *"---------------------------------------------------------------------- *"*"本地接口: *" IMPORTING *" VALUE(IM_ZGZ) TYPE ZTR_GZ OPTIONAL *" EXPORTING *" REFERENCE(EX_WHERE_CLAUSES) TYPE RSDS_TWHERE *"---------------------------------------------------------------------- *IM_ZGZ = '' 或者不存在 表示创建 DATA: LT_WHERE_CLAUSES TYPE RSDS_TWHERE, LS_WHERE_CLAUSES TYPE LINE OF RSDS_TWHERE, LT_WHERE_TAB TYPE TABLE OF RSDSWHERE, LS_WHERE_TAB TYPE RSDSWHERE, LT_FIELD_RANGES TYPE RSDS_TRANGE. DATA: WHERE_CLAUSES TYPE RSDS_TWHERE, EXPRESSIONS TYPE RSDS_TEXPR, FIELD_RANGES TYPE RSDS_TRANGE, NUMBER_OF_ACTIVE_FIELDS TYPE SY-TFILL, LT_TABLES_TAB TYPE STANDARD TABLE OF RSDSTABS, LS_TABLES_TAB TYPE RSDSTABS, TABFIELDS_NOT_DISPLAY TYPE STANDARD TABLE OF RSDSFIELDS, LT_FIELDS_TAB TYPE STANDARD TABLE OF RSDSFIELDS, LS_FIELDS_TAB TYPE RSDSFIELDS, FIELD_DESC TYPE STANDARD TABLE OF FLDCONVERT, FIELD_TEXTS TYPE STANDARD TABLE OF RSDSTEXTS, EVENTS TYPE STANDARD TABLE OF RSDSEVENTS, EVENT_FIELDS TYPE STANDARD TABLE OF RSDSEVFLDS, FIELDS_NOT_SELECTED TYPE STANDARD TABLE OF RSDSFIELDS, NO_INT_CHECK TYPE STANDARD TABLE OF RSDSTABS, ALV_QINFO TYPE STANDARD TABLE OF LVC_S_QINF, SELECTION_ID TYPE RSDYNSEL-SELID. DATA: L_ZGZ TYPE ZTR_GZ, L_ZGZNO TYPE ZTR_GZNO. SELECT * INTO TABLE @DATA(LT_ZTRT3023_25) FROM ZTRT3023_25 WHERE ZGZ = @IM_ZGZ. SORT LT_ZTRT3023_25 BY TABNAME ZGZNO. CLEAR:LS_WHERE_CLAUSES. LOOP AT LT_ZTRT3023_25 INTO DATA(LS_ZTRT3023_25). IF LS_ZTRT3023_25-TABNAME <> LS_WHERE_CLAUSES-TABLENAME AND LS_WHERE_CLAUSES-TABLENAME <> ''. LS_WHERE_CLAUSES-WHERE_TAB = LT_WHERE_TAB. APPEND LS_WHERE_CLAUSES TO LT_WHERE_CLAUSES. CLEAR: LT_WHERE_CLAUSES,LT_WHERE_TAB,LS_WHERE_CLAUSES,LS_WHERE_TAB . ENDIF. LS_WHERE_CLAUSES-TABLENAME = LS_ZTRT3023_25-TABNAME. LS_WHERE_TAB-LINE = LS_ZTRT3023_25-ZWHERE. APPEND LS_WHERE_TAB TO LT_WHERE_TAB. AT LAST. LS_WHERE_CLAUSES-WHERE_TAB = LT_WHERE_TAB. APPEND LS_WHERE_CLAUSES TO LT_WHERE_CLAUSES. ENDAT. ENDLOOP. CALL FUNCTION 'FREE_SELECTIONS_WHERE_2_RANGE' EXPORTING WHERE_CLAUSES = LT_WHERE_CLAUSES IMPORTING FIELD_RANGES = LT_FIELD_RANGES EXCEPTIONS EXPRESSION_NOT_SUPPORTED = 1 INCORRECT_EXPRESSION = 2 OTHERS = 3. IF SY-SUBRC <> 0. MESSAGE '读取选择条件错误' TYPE 'I' DISPLAY LIKE 'E'. RETURN. ENDIF. LOOP AT LT_FIELD_RANGES INTO DATA(LS_FIELD_RANGES). LS_FIELDS_TAB-TABLENAME = LS_FIELD_RANGES-TABLENAME. LOOP AT LS_FIELD_RANGES-FRANGE_T INTO DATA(LS_FIELD). LS_FIELDS_TAB-FIELDNAME = LS_FIELD-FIELDNAME. APPEND LS_FIELDS_TAB TO LT_FIELDS_TAB. ENDLOOP. ENDLOOP. SELECT * INTO TABLE @DATA(LT_ZTRT3023_20) FROM ZTRT3023_20. LOOP AT LT_ZTRT3023_20 INTO DATA(LS_ZTRT3023_20) . LS_TABLES_TAB-PRIM_TAB = LS_ZTRT3023_20-TABNAME. APPEND LS_TABLES_TAB TO LT_TABLES_TAB. ENDLOOP. CALL FUNCTION 'FREE_SELECTIONS_INIT' EXPORTING KIND = 'T' FIELD_RANGES_INT = LT_FIELD_RANGES " 选择屏幕初始值 IMPORTING SELECTION_ID = SELECTION_ID TABLES TABLES_TAB = LT_TABLES_TAB "如果kind为T,则定义选择屏幕字段来源表 TABFIELDS_NOT_DISPLAY = TABFIELDS_NOT_DISPLAY FIELDS_TAB = LT_FIELDS_TAB "如果kind为T,则为默认选择字段,为F则为可选选择字段清单 FIELD_DESC = FIELD_DESC FIELD_TEXTS = FIELD_TEXTS EVENTS = EVENTS " 选择屏幕事件进行逻辑处理 EVENT_FIELDS = EVENT_FIELDS FIELDS_NOT_SELECTED = FIELDS_NOT_SELECTED NO_INT_CHECK = NO_INT_CHECK ALV_QINFO = ALV_QINFO 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 <> 0. MESSAGE '初始化选择条件错误' TYPE 'I' DISPLAY LIKE 'E'. RETURN. ENDIF. CALL FUNCTION 'FREE_SELECTIONS_DIALOG' EXPORTING SELECTION_ID = SELECTION_ID TITLE = 'Free Selection' STATUS = '1' "选择屏幕状态 AS_WINDOW = 'X' " 是否弹窗 TREE_VISIBLE = 'X' "选择树是否可见 IMPORTING WHERE_CLAUSES = WHERE_CLAUSES "选择屏幕返回的选择条件,自动处理的where条件表,可直接使用 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 <> 0. MESSAGE '规则 没有被修改保存' TYPE 'I'. RETURN. ENDIF. EX_WHERE_CLAUSES = WHERE_CLAUSES. *--------------------------------------------------------------------* *保存选择条件到自定义表中。 CLEAR: LT_ZTRT3023_25,LS_ZTRT3023_25,L_ZGZNO. LOOP AT WHERE_CLAUSES INTO DATA(LS_WHERE) . LS_ZTRT3023_25-ZGZ = IM_ZGZ. LS_ZTRT3023_25-TABNAME = LS_WHERE-TABLENAME. LOOP AT LS_WHERE-WHERE_TAB INTO DATA(LINE). L_ZGZNO = L_ZGZNO + 10. LS_ZTRT3023_25-ZGZNO = L_ZGZNO. LS_ZTRT3023_25-ZWHERE = LINE. APPEND LS_ZTRT3023_25 TO LT_ZTRT3023_25. ENDLOOP. ENDLOOP. DELETE FROM ZTRT3023_25 WHERE ZGZ = @IM_ZGZ . MODIFY ZTRT3023_25 FROM TABLE LT_ZTRT3023_25. COMMIT WORK. ENDFUNCTION. |
在使用WHERE条件可如下面的操作
代码中的LT_CONDS,就是前面的WHERE_CLAUSES的值 。
1 2 3 4 5 6 7 8 9 |
TRY. SELECT COUNT(*) FROM ZTRT_BANK_TRANS WHERE (LT_CONDS) INTO @L_COUNT. CATCH CX_SY_DYNAMIC_OSQL_ERROR. MESSAGE E106(ZTR203) WITH LS_ZTRT3023_25-ZGZ ."DISPLAY LIKE 'E'. RETURN. ENDTRY. |