复选框多选过滤器
Checkbox multiple selection filter
我想根据是否选中复选框将数据过滤到我的程序中。
parameters: p_z1 as checkbox DEFAULT 'X' ,
p_z2 as checkbox.
selection-screen end of block b4.
那么,如果选择了这两者之一,或者如果两者都被选择了,我该如何过滤我的数据?
select single * from mara where matnr = pv_matnr
and "if checkbox one is selected" where matkl = t023-matkl.
"if checkbox two is selected" where matkl = v023-matkl.
您可以准备动态 where
子句 -
DATA : lv_query TYPE string.
CONCATENATE 'MATNR = PV_MATNR' lv_query INTO lv_query.
IF p_z1 = 'X'.
CONCATENATE lv_query 'AND MATKL = T023-MATKL' INTO lv_query SEPARATED BY space.
ENDIF.
IF p_z2 = 'X'.
CONCATENATE lv_query 'AND MATKL = V023-MATKL' INTO lv_query SEPARATED BY space.
ENDIF.
SELECT SINGLE * FROM mara WHERE (lv_query).
"Dynamic" 查询应尽可能避免,只是为了更容易检查代码是否受到可能的 SQL 注入攻击(使用 SELECT … WHERE (lv_query), 空 lv_query 可能是拒绝服务攻击)。所以我会通过 divScorp 编写代码如下:
parameters: p_z1 as checkbox DEFAULT 'X' ,
p_z2 as checkbox,
pv_matnr TYPE mara-matnr.
DATA: t023 TYPE t023, v023 TYPE v023, mara TYPE mara.
DATA range_matkl TYPE RANGE OF mara-matkl.
CLEAR range_matkl.
IF p_z1 = 'X'.
APPEND VALUE #( sign = 'I' option = 'EQ' low = t023-matkl ) TO range_matkl.
ENDIF.
IF p_z2 = 'X'.
APPEND VALUE #( sign = 'I' option = 'EQ' low = v023-matkl ) TO range_matkl.
ENDIF.
SELECT SINGLE * FROM mara WHERE MATNR = PV_MATNR and matkl IN range_matkl.
PS:我的代码(和 divScorp 的代码)是无意义的,因为 pureAbap 算法不正确:如果 p_z1 和 p_z2 都是 'X' ,那么如果 t023-matkl 与 v023-matkl 不同,则存在 SELECT returns 什么都没有的风险。也许 p_z1 和 p_z2 应该转换成单选按钮? (两者只能选其一?)
您可以随时使用:
IF chck1 = 'X' and chck2 = 'X'.
SELECT ... WHERE both.
ELSEIF chck1 = 'X'.
SELECT ... WHERE ...
ELSEIF chck2 = 'X'.
SELECT ... WHERE ...
ENDIF.
但我想那不是你想要做的。不是最优雅的方式,但嘿 - 它有效。
我想根据是否选中复选框将数据过滤到我的程序中。
parameters: p_z1 as checkbox DEFAULT 'X' ,
p_z2 as checkbox.
selection-screen end of block b4.
那么,如果选择了这两者之一,或者如果两者都被选择了,我该如何过滤我的数据?
select single * from mara where matnr = pv_matnr
and "if checkbox one is selected" where matkl = t023-matkl.
"if checkbox two is selected" where matkl = v023-matkl.
您可以准备动态 where
子句 -
DATA : lv_query TYPE string.
CONCATENATE 'MATNR = PV_MATNR' lv_query INTO lv_query.
IF p_z1 = 'X'.
CONCATENATE lv_query 'AND MATKL = T023-MATKL' INTO lv_query SEPARATED BY space.
ENDIF.
IF p_z2 = 'X'.
CONCATENATE lv_query 'AND MATKL = V023-MATKL' INTO lv_query SEPARATED BY space.
ENDIF.
SELECT SINGLE * FROM mara WHERE (lv_query).
"Dynamic" 查询应尽可能避免,只是为了更容易检查代码是否受到可能的 SQL 注入攻击(使用 SELECT … WHERE (lv_query), 空 lv_query 可能是拒绝服务攻击)。所以我会通过 divScorp 编写代码如下:
parameters: p_z1 as checkbox DEFAULT 'X' ,
p_z2 as checkbox,
pv_matnr TYPE mara-matnr.
DATA: t023 TYPE t023, v023 TYPE v023, mara TYPE mara.
DATA range_matkl TYPE RANGE OF mara-matkl.
CLEAR range_matkl.
IF p_z1 = 'X'.
APPEND VALUE #( sign = 'I' option = 'EQ' low = t023-matkl ) TO range_matkl.
ENDIF.
IF p_z2 = 'X'.
APPEND VALUE #( sign = 'I' option = 'EQ' low = v023-matkl ) TO range_matkl.
ENDIF.
SELECT SINGLE * FROM mara WHERE MATNR = PV_MATNR and matkl IN range_matkl.
PS:我的代码(和 divScorp 的代码)是无意义的,因为 pureAbap 算法不正确:如果 p_z1 和 p_z2 都是 'X' ,那么如果 t023-matkl 与 v023-matkl 不同,则存在 SELECT returns 什么都没有的风险。也许 p_z1 和 p_z2 应该转换成单选按钮? (两者只能选其一?)
您可以随时使用:
IF chck1 = 'X' and chck2 = 'X'.
SELECT ... WHERE both.
ELSEIF chck1 = 'X'.
SELECT ... WHERE ...
ELSEIF chck2 = 'X'.
SELECT ... WHERE ...
ENDIF.
但我想那不是你想要做的。不是最优雅的方式,但嘿 - 它有效。