Dynamic WHERE clause in SELECT statement
Hi,
I need to extract (SELECT) all the products in different salesorganizations. Since a product can be available in more than 1 salesorg I have created several properties in the PRODUCT dimension - 1 for each salesorganization (naming: Sxxxx where xxxx is the salesorganization number).
Since I need to prefix the salesorganization property with an "S" I have created a property on the SALESORG dimension called SALESORG.
Therefore I need to create a dynamic WHERE clause in the SELECT statement. Currently my script is:
*SELECT(%SORG%, "[SALESORG]",SALESORG, [ID]=%SALESORG_SET%)
*SELECT(%PROD%, "[ID]",PRODUCT, [%SORG%]="X")
My first SELECT find the Sxxx (equal to the property I need in the PRODUCT dimension). My second SELECT uses the variable in the first SELCT statement to use the correct property for the WHERE clause.
Unfortunately the code is not validated - any suggestions?
/Lars
Hi Lars,
If you run it from a DM package without validating it, does it still work? I would bet it does.
If this is the case I would open a message with SAP (it would be an enhancement request). Until they fix the validation code, you would just have to live with the script not validating.
Cheers,
Ethan
Similar Messages
-
WHERE clause in SELECT statement
hi experts..
i want to give 'OR' condition in the 'where' clause of 'SELECT' statement.
is it possible?
for examlpe..
IF EXIDV2 IS NOT INITIAL.
SELECT * FROM YSDT_SHIPLOAD
INTO TABLE IG_SHIPLOAD
WHERE EXIDV2 = EXIDV2 AND
VHILM = PC1 OR PC2 OR PC3.
ENDIF.
i want that VHILM should be one of those three.
how can i do this?
thanks..Hi ,
its possible,
Select * from ysdt_shipload int table ig_shipload where exidv2 = exidv2
AND vhilm = pc1
OR vhilm = pc2
OR vhilm = pc3.
OR
Select * from ysdt_shipload int table ig_shipload where exidv2 = exidv2
AND (vhilm = pc1 or vhilm = pc2 or vhilm = pc3).
Regards
Arani Bhaskar
Edited by: arani bhaskar on Mar 16, 2009 5:14 PM -
Dynamic where clause with loop statement
Hi all,
is it possible to use a dynamic where clause with a loop statement?
Can you please advise me, how the syntax needs to be?
Thanks for your suggestions,
kind regards, Kathrin!Hi Kathrin,
If u are in ECC 6.0, please go through the code...
REPORT zdynamic_select.
TYPES:
BEGIN OF ty_sales,
vbeln TYPE vbak-vbeln, " Sales document
posnr TYPE vbap-posnr, " Sales document item
matnr TYPE vbap-matnr, " Material number
arktx TYPE vbap-arktx, " Short text for sales order item
kwmeng TYPE vbap-kwmeng, " Order quantity
vkorg TYPE vbak-vkorg, " Sales organization
kunnr TYPE vbak-kunnr, " Sold-to party
netwr TYPE vbak-netwr, " Net Value of the Sales Order
END OF ty_sales.
DATA :
gt_sales TYPE STANDARD TABLE OF ty_sales,
wa_sales TYPE ty_sales.
DATA: ob_select TYPE REF TO cl_rs_where.
DATA: ob_from TYPE REF TO cl_rs_where.
DATA: ob_where TYPE REF TO cl_rs_where,
gv_source TYPE abapsource.
START-OF-SELECTION.
*Step 1 : Prepare the select fields.
PERFORM zf_build_select.
*Step 2 : Build the from clause for the select
PERFORM zf_build_from.
*Step 3 : Build the where clause for the select
PERFORM zf_build_where.
*Step 4 : Execute the dynamic select
SELECT (ob_select->n_t_where)
FROM (ob_from->n_t_where)
INTO CORRESPONDING FIELDS OF TABLE gt_sales
WHERE (ob_where->n_t_where).
LOOP AT gt_sales INTO wa_sales.
WRITE : /5 wa_sales-vbeln,
15 wa_sales-vkorg,
20 wa_sales-kunnr,
40 wa_sales-netwr,
50 wa_sales-posnr,
60 wa_sales-matnr,
70 wa_sales-arktx,
90 wa_sales-kwmeng.
ENDLOOP.
*& Form zf_build_select
FORM zf_build_select .
CREATE OBJECT ob_select.
*Build the table name/field name combination
*Add Sales order header fields
CLEAR gv_source.
CALL METHOD cl_rs_where=>build_tabname_fieldname
EXPORTING
i_tabname = 'VBAK'
i_fieldname = 'VBELN'
i_sign = '~'
IMPORTING
e_combined = gv_source.
*Add the where line
CALL METHOD ob_select->add_line
EXPORTING
i_line = gv_source.
CLEAR gv_source.
CALL METHOD cl_rs_where=>build_tabname_fieldname
EXPORTING
i_tabname = 'VBAK'
i_fieldname = 'VKORG'
i_sign = '~'
IMPORTING
e_combined = gv_source.
*Add the where line
CALL METHOD ob_select->add_line
EXPORTING
i_line = gv_source.
CLEAR gv_source.
CALL METHOD cl_rs_where=>build_tabname_fieldname
EXPORTING
i_tabname = 'VBAK'
i_fieldname = 'KUNNR'
i_sign = '~'
IMPORTING
e_combined = gv_source.
*Add the where line
CALL METHOD ob_select->add_line
EXPORTING
i_line = gv_source.
CLEAR gv_source.
CALL METHOD cl_rs_where=>build_tabname_fieldname
EXPORTING
i_tabname = 'VBAK'
i_fieldname = 'NETWR'
i_sign = '~'
IMPORTING
e_combined = gv_source.
*Add the where line
CALL METHOD ob_select->add_line
EXPORTING
i_line = gv_source.
*Add Sales order item fields
CALL METHOD cl_rs_where=>build_tabname_fieldname
EXPORTING
i_tabname = 'VBAP'
i_fieldname = 'POSNR'
i_sign = '~'
IMPORTING
e_combined = gv_source.
*Add the where line
CALL METHOD ob_select->add_line
EXPORTING
i_line = gv_source.
CLEAR gv_source.
CALL METHOD cl_rs_where=>build_tabname_fieldname
EXPORTING
i_tabname = 'VBAP'
i_fieldname = 'MATNR'
i_sign = '~'
IMPORTING
e_combined = gv_source.
*Add the where line
CALL METHOD ob_select->add_line
EXPORTING
i_line = gv_source.
CLEAR gv_source.
CALL METHOD cl_rs_where=>build_tabname_fieldname
EXPORTING
i_tabname = 'VBAP'
i_fieldname = 'ARKTX'
i_sign = '~'
IMPORTING
e_combined = gv_source.
*Add the where line
CALL METHOD ob_select->add_line
EXPORTING
i_line = gv_source.
CLEAR gv_source.
CALL METHOD cl_rs_where=>build_tabname_fieldname
EXPORTING
i_tabname = 'VBAP'
i_fieldname = 'KWMENG'
i_sign = '~'
IMPORTING
e_combined = gv_source.
*Add the where line
CALL METHOD ob_select->add_line
EXPORTING
i_line = gv_source.
ENDFORM. " zf_build_select
*& Form zf_build_from
FORM zf_build_from .
CREATE OBJECT ob_from.
*Add opening bracket
CALL METHOD ob_from->add_opening_bracket
CLEAR gv_source.
*Add the join condition.This can be made
*fully dynamic as per your requirement
gv_source = 'VBAK AS VBAK INNER JOIN VBAP AS VBAP'.
*Add the where line
CALL METHOD ob_from->add_line
EXPORTING
i_line = gv_source.
CLEAR gv_source.
*Add the join condition.This can be made
*fully dynamic as per your requirement
gv_source = 'ON VBAKVBELN = VBAPVBELN'.
*Add the where line
CALL METHOD ob_from->add_line
EXPORTING
i_line = gv_source.
*Add the closing bracket
CALL METHOD ob_from->add_closing_bracket
ENDFORM. " zf_build_from
*& Form zf_build_where
FORM zf_build_where .
DATA :
lv_field TYPE REF TO data,
lv_field_low TYPE REF TO data,
lv_field_high TYPE REF TO data.
CREATE OBJECT ob_where.
*Add the field VBELN : Sales Document
*Use this method if you want to assign a single value to a field
*Set the value for VBELN : Sales Document Number
CALL METHOD ob_where->add_field
EXPORTING
i_fieldnm = 'VBAK~VBELN'
i_operator = '='
i_intlen = 10
i_datatp = 'CHAR'
IMPORTING
e_r_field = lv_field.
CALL METHOD ob_where->set_value_for_field
EXPORTING
i_fieldnm = 'VBAK~VBELN'
i_value = '0000120020'.
*Use this method if you want to assign a range of values
*Set a range for the Sales Document number
CALL METHOD ob_where->add_field_between_2values
EXPORTING
i_fieldnm = 'VBAK~VBELN'
i_intlen = 10
i_datatp = 'CHAR'
IMPORTING
e_r_field_low = lv_field_low
e_r_field_high = lv_field_high.
CALL METHOD ob_where->set_2values_for_field
EXPORTING
i_fieldnm = 'VBAK~VBELN'
i_value_low = '0000120020'
i_value_high = '0000120067'.
*Set the 'AND' Clause
CALL METHOD ob_where->add_and.
*Add the field MATNR : Material
CALL METHOD ob_where->add_field
EXPORTING
i_fieldnm = 'MATNR'
i_operator = '='
i_intlen = 18
i_datatp = 'CHAR'
IMPORTING
e_r_field = lv_field.
*Set the value for the Material field
CALL METHOD ob_where->set_value_for_field
EXPORTING
i_fieldnm = 'MATNR'
i_value = '000000000050111000'.
*Set the 'AND' Clause
CALL METHOD ob_where->add_and
*Add the field VKORG
CALL METHOD ob_where->add_field
EXPORTING
i_fieldnm = 'VKORG'
i_operator = '='
i_intlen = 4
i_datatp = 'CHAR'
IMPORTING
e_r_field = lv_field.
*Set the value for VKORG : Sales Organization
CALL METHOD ob_where->set_value_for_field
EXPORTING
i_fieldnm = 'VKORG'
i_value = 'GMUS'.
ENDFORM. " zf_build_where -
Dynamic where clause in select query
Hi
I'm trying for a dynamic query and in the dynamic where clause i'm checking a field for constant.....for instance if i wanna to use WERKS EQ '0478' how to declare it in dync where clause. i tried for the following:
SELECT (TAB_FIELD) INTO TABLE DITAB UP TO 10 ROWS FROM (TAB_NAME) WHERE (CONDI).
heree CONDI = 'werks eq 0287' . in this case the query fails as the werks shud be equal to '0287' and not just 0287. How to handle this? i tried for the following
CONDI = 'werks eq " '0287' " ' but no luck as it is not a valid stmt as i knew.hi prabhu,
this is how u select dynamically...
DATA: COND(72) TYPE C,
ITAB LIKE TABLE OF COND.
PARAMETERS: CITY1(10) TYPE C, CITY2(10) TYPE C.
DATA WA TYPE SPFLI-CITYFROM.
CONCATENATE 'CITYFROM = ''' CITY1 '''' INTO COND.
APPEND COND TO ITAB.
CONCATENATE 'OR CITYFROM = ''' CITY2 '''' INTO COND.
APPEND COND TO ITAB.
CONCATENATE 'OR CITYFROM = ''' 'BERLIN' '''' INTO COND.
APPEND COND TO ITAB.
LOOP AT ITAB INTO COND.
WRITE COND.
ENDLOOP.
SKIP.
SELECT CITYFROM
INTO WA
FROM SPFLI
WHERE (ITAB).
WRITE / WA.
ENDSELECT.
regards,
sohi -
How to create VO with multiple dynamic where clauses on select with UNION?
I am trying to implement the View Object for the UNION query that looks like this:
select a,b,c...
from t1,t2,...
where dynamic_where_clause1
union all
select a,b,c,...
from t11,t12, ...
where dynamic_where_clause2
There are up to 60 input parameters that are used to generate dynamic where clauses. They are actually created by calling PL SQL function.
So far, I was not able to assign both where clauses to the view object. Is there a workable solution for this problem, besides resorting to programmatic View Object?
I understand that recommended way with UNIONs is to wrap both queries into a parent select:
select * from (
select a,b,c...
from t1,t2,...
where ... -- table relationship joints
union all
select a,b,c,...
from t11,t12, ...
where ... -- table relationship joints
) QRSLT
where dynamic_where_clause
Unfortunately this approach doesn't work here, since individual selects are producing unmanageable amount of data and resulting query takes forever to complete.I afraid I would not have any real benefits from using VO if I replace the entire query with every request. Actually, the performance may suffer.
I solved the problem by creating a POJO Data Control and invoking the custom select query from java. Not sure if it is the best approach to the problem, but implementation time is limited and it works.
Actually, this is not the first time I see the need to implement VO with complicated SQL like select with unions and dynamic pieces. It would be nice to find a solution and not resort to workarounds.
Edited by: viksicom on Aug 2, 2012 8:48 AM -
Dynamic where condition in Select statement
Hi,
I have 10 fields on selection-screeen. In which ever field the user enters single values or ranges,i should pick that field dynamically and pass that field along with value range to Where condition of Select statement.How can i achieve this? Please help.
Regards
K Srinivassee the following example:
data : begin of itab occurs 0,
matnr like mara-matnr,
end of itab.
ypes: begin of ty_s_clause.
types: line(72) type c.
types: end of ty_s_clause.
data : begin of gt_condtab occurs 0.
include structure hrcond.
data : end of gt_condtab.
FIELD-SYMBOLS <fs_wherecond> TYPE ty_s_clause.
data:
gt_where_clauses type standard table of ty_s_clause
with default key.
gt_condtab-field = 'MATNR'.
gt_condtab-opera = 'EQ'.
gt_condtab-low = '000000000000000111'.
append gt_condtab.
clear gt_condtab.
call function 'RH_DYNAMIC_WHERE_BUILD'
exporting
dbtable = space " can be empty
tables
condtab = gt_condtab
where_clause = gt_where_clauses
exceptions
empty_condtab = 01
no_db_field = 02
unknown_db = 03
wrong_condition = 04.
select matnr from mara into table itab where (gt_where_clauses). -
How build where clause in select statement in FM for Virtual provider
Hi
I looking for example of FM for Virtual provider where I find code how assign to select statement "where" clause value from query variable.
In following code how build t_r_custtype range and how assign value to it.
CODE********************************
TYPE-POOLS: abap.
initialize
CLEAR: e_t_data, e_t_msg.
this is specific to infoprovider VIRTPROV
CHECK i_infoprov = 'VIRTPROV'.
FIELD-SYMBOLS: <l_s_sbook> TYPE sbook,
<l_s_data> TYPE ANY.
DATA: l_t_component TYPE abap_compdescr_tab,
l_t_sbook TYPE TABLE OF sbook.
initialize
CLEAR e_t_data.
Data selection / only Business Customer
SELECT * FROM sbook
INTO CORRESPONDING FIELDS OF TABLE l_t_sbook
WHERE custtype in t_r_custtype.
ENDCODE********************************
Thanks a lot
AdamHello,
Would you like fill the ranges in Customer exit for BEx..?
If Yes. please refer the attachment for the whole code...
"Sample code in Customer Exit in BEx"
IF i_step = 2.
CASE i_vnam.
WHEN 'ZDAY_CX'.
LOOP AT i_t_var_range INTO loc_var_range WHERE vnam = 'ZDAY_IN'.
CLEAR: l_s_range.
ZT_DT1 = loc_var_range-low.
ZT_DT2 = loc_var_range-HIGH.
CALL FUNCTION 'DATE_CREATE'
EXPORTING
ANZAHL_JAHRE = 0
ANZAHL_KALTAGE = 0
ANZAHL_MONATE = '-1'
ANZAHL_TAGE = 0
DATUM_EIN = ZT_DT1
DATUM_EIN_ULT = ' '
ULTIMO_SETZEN = ' '
IMPORTING
DATUM_AUS = ZFIDAY .
E_TT =
E_ULTKZ =
CALL FUNCTION 'DATE_CREATE'
EXPORTING
ANZAHL_JAHRE = 0
ANZAHL_KALTAGE = 0
ANZAHL_MONATE = '-1'
ANZAHL_TAGE = 0
DATUM_EIN = ZT_DT2
DATUM_EIN_ULT = ' '
ULTIMO_SETZEN = ' '
IMPORTING
DATUM_AUS = ZLSDAY.
E_TT =
E_ULTKZ =
l_s_range-low = ZFIDAY .
l_s_range-high = ZLSDAY .
l_s_range-sign = 'I'.
l_s_range-opt = 'EQ'.
APPEND l_s_range TO e_t_range.
ENDLOOP.
*****************************************End*************************************
**To get the From date (For Text Variable) as per the user input date interval range**
WHEN 'ZR_S'.
LOOP AT i_t_var_range INTO loc_var_range WHERE vnam = 'ZDAY_IN'.
CLEAR: l_s_range.
ZT_DT1 = loc_var_range-low.
ZT_DT2 = loc_var_range-HIGH.
CALL FUNCTION 'DATE_CREATE'
EXPORTING
ANZAHL_JAHRE = 0
ANZAHL_KALTAGE = 0
ANZAHL_MONATE = 0
ANZAHL_TAGE = 0
DATUM_EIN = ZT_DT1
DATUM_EIN_ULT = ' '
ULTIMO_SETZEN = ' '
IMPORTING
DATUM_AUS = ZFIDAY .
E_TT =
E_ULTKZ =
l_s_range-low0(2) = ZFIDAY6(2).
l_s_range-low+2(1) = '.'.
l_s_range-low3(2) = ZFIDAY4(2).
l_s_range-low+5(1) ='.'.
l_s_range-low6(4) = ZFIDAY0(4).
l_s_range-sign = 'I'.
l_s_range-opt = 'EQ'.
APPEND l_s_range TO e_t_range.
ENDLOOP.
*****************************************End*************************************
Please let me know if any clarification required..
Rinku.. -
Use of LIKE in where clause of select statement for multiple records
Hi Experts,
I have a account number field which is uploaded from a file. Now this account numbers uploaded does not match fully with sap table account numbers but it contains all of the numbers provided in the file mostly in the upright positions.
For example in file we have account number as 2ARS1 while in sap table the value is 002ARS1.
And i want to fetch data from sap table based on account number uploaded. So, i am trying to use LIKE with for all entries but its not working as mentioned below but LIKE is not working with FOR ALL ENTRIES.
data : begin of t_dda occurs 0,
dda(19) type c,
end of t_dda.
data : begin of t_bukrs occurs 0,
bukrs type t012k-bukrs,
end of t_bukrs.
data : dda type t012k-bankn,
w_dda type t012k-bankn.
CONCATENATE '%'
'2ARS1'
INTO W_DDA.
MOVE W_DDA TO T_DDA-DDA.
APPEND T_DDA.
CLEAR T_DDA.
free t_bukrs.
SELECT BUKRS
FROM T012K
into TABLE t_bukrs
for all entries in t_dda
WHERE BANKN like t_dda-dda.
Can anybody suggest what should i use to get the data for multiple account numbers using one select statement only instead on using SELECT UP TO 1 ROWS in LOOP....ENDLOOP ?
Thanks in advance,
AkashHi,
yes, For All entries won't work for LIKE with '% '.
I think the other alternative is go for Native SQL by writing sub-query
sample code is here:
data: begin of i_mara occurs 0,
matnr like mara-matnr,
matkl like mara-matkl,
end of i_mara.
exec sql.
select matnr, matkl from mara where matnr in (select matnr from marc) and matnr like '%ma' into :i_mara
endexec.
loop at i_mara.
write:/ i_mara-matnr, i_mara-matkl.
endloop.
hope u got it.
regards
Mahesh
Edited by: Mahesh Reddy on Jan 21, 2009 2:32 PM -
Dynamic Where Clause in select using member attribute of class
Hello,
can anybody provide me some tips how to solve this problem:
SELECT (lv_sql_select) INTO CORRESPONDING FIELDS OF TABLE lt_cntrl2
FROM (lv_db_name)
FOR ALL ENTRIES IN mt_guids
WHERE (lv_sql_where).
works perfectly fine if I use a local table variable or a member of my class. But if I build my where clause using a member attribute of my class which comes from the interace- exactly the same type and data as before I get a dump.
SELECT (lv_sql_select) INTO CORRESPONDING FIELDS OF TABLE lt_xyz
FROM (lv_db_name)
FOR ALL ENTRIES IN if_abc~mt_guids
my where clause "guid = if_abc~mt_guids-guid.
Dump:
Error analysis
An exception occurred that is explained in detail below.
The exception, which is assigned to class 'CX_SY_DYNAMIC_OSQL_SEMANTICS', was
not caught in
procedure "....GET_DATA" "(METHOD)", nor was it propagated
by a RAISING clause.
Since the caller of the procedure could not have anticipated that the
exception would occur, the current program is terminated.
The reason for the exception is:
Das laufende ABAP-Programm wollte eine Open SQL-Anweisung ausführen,
die eine dynamische Angabe enthält. Der Parser lieferte jedoch den
Fehler: "Wrong table name or table alias name table alias name
table alias name "....". table alias name
"...". table alias name "...". t"Hi Marco Freischlag,
As per my understanding , if we give for all entries we want to use internal table , For where conditions you want to pass variable , not internal table one filed,because it will not have header line...
so code like this...
SELECT (lv_sql_select) INTO CORRESPONDING FIELDS OF TABLE lt_xyz
FROM (lv_db_name)
FOR ALL ENTRIES IN if_abc~mt_guids
where guid = if_abc~mt_guids-guid. " Here pass some variable or work area in it and test.
Thanks,
Vijay SR -
Dynamic IF statement...like dynamic where clause...
Hi,
I know how to do a dynamic where clause by putting (varname) like shown below -
select * from cust_mstr where (varname).
Can I do it something like this in IF statement.
if (varname).
endif.
The varname will have different conditions.
Your help will be greately appreciated.
Regards,
SumeHello Sume,
I would try to use an approach using ranges:
DATA:
gv_material TYPE matnr.
gv_material = '292-392-392-202'.
IF gv_material = '292-392-392-202'.
WRITE: / `Static check:`, gv_material, `matches!`.
ELSE.
WRITE: / `Static check:`, gv_material, `doesn't match!`.
ENDIF.
PERFORM dynamic_check
USING
gv_material
'I'
'EQ'
'292-392-392-202'
space.
PERFORM dynamic_check
USING
gv_material
'I'
'CP'
'*292*'
space.
PERFORM dynamic_check
USING
gv_material
'I'
'CP'
'*ABC*'
space.
*& Form dynamic_check
FORM dynamic_check USING pv_value TYPE clike
pv_sign TYPE ddsign
pv_option TYPE ddoption
pv_low TYPE string
pv_high TYPE string.
DATA lr_range TYPE RANGE OF string.
DATA ls_range LIKE LINE OF lr_range.
ls_range-sign = pv_sign.
ls_range-option = pv_option.
ls_range-low = pv_low.
ls_range-high = pv_high.
APPEND ls_range TO lr_range.
IF pv_value IN lr_range.
WRITE: / `Dynamic check matches: `, pv_value, ls_range-sign, ls_range-option, ls_range-low, ls_range-high.
ELSE.
WRITE: / `Dynamic check doesn't match:`, pv_value, ls_range-sign, ls_range-option, ls_range-low, ls_range-high.
ENDIF.
ENDFORM. "dynamic_check
For values of SIGN and OPTION check the values of the domains used in the data elements of the parameters of form dynamic_check.
Output is like:
Static check: 292-392-392-202 matches!
Dynamic check matches: 292-392-392-202 I EQ 292-392-392-202
Dynamic check matches: 292-392-392-202 I CP *292*
Dynamic check doesn't match: 292-392-392-202 I CP *ABC*
Edited by: Alejiandro Sensejl on Aug 11, 2010 8:23 PM:
Unfortunately I don't know why the code-tag isn't working, I sent a mail to SCN tech-team about this... Sorry, but you have to copy the code somehow to have a look at it -
Regarding dynamically assigning the where clause to select query
hi,
Please send the code regarding how to dynamically assign the where clause to select query.
thanks in advanceSELECT <fileds>
INTO TABLE itab
FROM dbase
WHERE condition. -
Dynamic where clause in my query
I am using a view
select * from vw_pt_inv_customer
My requirement is have a web page where users can search for customers by filling in a form which has the following feilds: - User can fill in the form with all the feilds or not. I want a dynamic where clause.. how can i get this work for me?
Title
Firstname
Surname
Address1
Address2
Postcode
Telephone No879796 wrote:
I am using a view
select * from vw_pt_inv_customer
My requirement is have a web page What web frame work is used? Apex? Something else?
where users can search for customers by filling in a form which has the following feilds: - User can fill in the form with all the feilds or not. I want a dynamic where clause.. how can i get this work for me?Dynamic where clauses are a Very Bad Idea (tm).
And having an open ended search function on a web page is also not a great idea.
If you are using Apex, the for performance (and even query flexibility) it will be better creating a separate reporting region for each unique query. A boolean rendering condition checks the existing bind variables in order to determine if that specific reporting region should be executed.
This results in a reporting region having a proper and dedicated SQL query (no hacking of the where clause) and that region only being rendered for the proper combination of supplied filter criteria.
A slightly more complex, but more flexible approach, is using a generic reporting region that calls a PL/SQL function that creates returns the SQL query for execution and rendering. The approach to this is very similar to constructing a SQL ref cursor (dynamically) for a client. The only difference is that instead of creating the actual ref cursor, the code simply needs to return the SQL source code statement - with bind variables. The Apex run-time does the rest (does the binds and execution and rendering).
If you are not using Apex - you should still consider these approaches. And not hacking a single SQL to cater for all different types of filter conditions. -
Dynamic Where clause in 4.6C [Literals allowed but not variables]
Good afternoon-
We have a dynamic select statement that works perfectly in 4.7 ext 2, but is causing a short dump in 4.6C. The error in the short dump says:
The current ABAP/4 program attempted to execute an ABAP/4 Open SQL
statement containing a WHERE condition of the form WHERE (itab) or
WHERE ... AND (itab). The part of the WHERE condition specified at
runtime in the internal table itab contains an invalid
value "HDR_COMP_CODE". Literals are allowed as values, but not variables.
HDR_COMP_CODE is in incoming parameter to the function module that is calling this code. It is of type BKPF-BUKRS.
Here is the code:
DATA: BEGIN OF L_WHERE_CLAUSE OCCURS 0,
TEXT_LINE(60) TYPE C,
END OF L_WHERE_CLAUSE.
CLEAR L_WHERE_CLAUSE.
IF NOT HDR_COMP_CODE IS INITIAL.
SELECT SINGLE KTOPL
INTO LV_KTOPL
FROM T001
WHERE BUKRS = HDR_COMP_CODE.
L_WHERE_CLAUSE = 'B~BUKRS = HDR_COMP_CODE'.
APPEND L_WHERE_CLAUSE.
CLEAR L_WHERE_CLAUSE.
L_WHERE_CLAUSE = 'AND'.
APPEND L_WHERE_CLAUSE.
CLEAR L_WHERE_CLAUSE.
L_WHERE_CLAUSE = 'T~KTOPL = LV_KTOPL'.
APPEND L_WHERE_CLAUSE.
CLEAR L_WHERE_CLAUSE.
L_WHERE_CLAUSE = 'AND'.
APPEND L_WHERE_CLAUSE.
CLEAR L_WHERE_CLAUSE.
ENDIF.
L_WHERE_CLAUSE = 'T~SPRAS = SY-LANGU'.
APPEND L_WHERE_CLAUSE.
CLEAR L_WHERE_CLAUSE.
SELECT BSAKNR TTXT50
INTO (LT_GL_ACCOUNT_DATA-GL_ACCOUNT, LT_GL_ACCOUNT_DATA-DESCRIPTION)
FROM SKB1 AS B INNER JOIN SKAT AS T
ON TSAKNR = BSAKNR
WHERE (L_WHERE_CLAUSE).
APPEND LT_GL_ACCOUNT_DATA.
CLEAR LT_GL_ACCOUNT_DATA.
ENDSELECT.
This EXACT query works in 4.7, has anyone run into this, or can they tell me why I might be getting the error?
Thanks,
AndyWell, in this case, unless I'm missing something, you do not requre a dynamic WHERE clause. You can use RANGES instead.
report zrich_0001.
*DATA: BEGIN OF L_WHERE_CLAUSE OCCURS 0,
*TEXT_LINE(60) TYPE C,
*END OF L_WHERE_CLAUSE.
ranges: r_bukrs for t001-bukrs.
ranges: r_ktopl for t001-ktopl.
*CLEAR L_WHERE_CLAUSE.
IF NOT HDR_COMP_CODE IS INITIAL.
clear r_bukrs. refresh r_bukrs.
r_bukrs-sign = 'I'.
r_bukrs-option = 'EQ'.
r_bukrs-low = hdr_comp_code.
append r_bukrs.
SELECT SINGLE KTOPL
INTO LV_KTOPL
FROM T001
WHERE BUKRS = HDR_COMP_CODE.
clear r_ktopl. refresh r_ktopl.
r_ktopl-sign = 'I'.
r_ktopl-option = 'EQ'.
r_ktopl-low = lv_ktopl.
append r_ktopl.
*L_WHERE_CLAUSE = 'B~BUKRS = HDR_COMP_CODE'.
*APPEND L_WHERE_CLAUSE.
*CLEAR L_WHERE_CLAUSE.
*L_WHERE_CLAUSE = 'AND'.
*APPEND L_WHERE_CLAUSE.
*CLEAR L_WHERE_CLAUSE.
*L_WHERE_CLAUSE = 'T~KTOPL = LV_KTOPL'.
*APPEND L_WHERE_CLAUSE.
*CLEAR L_WHERE_CLAUSE.
*L_WHERE_CLAUSE = 'AND'.
*APPEND L_WHERE_CLAUSE.
*CLEAR L_WHERE_CLAUSE.
ENDIF.
*L_WHERE_CLAUSE = 'T~SPRAS = SY-LANGU'.
*APPEND L_WHERE_CLAUSE.
*CLEAR L_WHERE_CLAUSE.
SELECT B~SAKNR T~TXT50
INTO (LT_GL_ACCOUNT_DATA-GL_ACCOUNT, LT_GL_ACCOUNT_DATA-DESCRIPTION)
FROM SKB1 AS B INNER JOIN SKAT AS T
ON T~SAKNR = B~SAKNR
WHERE b~bukrs in r_bukrs
and t~ktopl in r_ktopl.
APPEND LT_GL_ACCOUNT_DATA.
CLEAR LT_GL_ACCOUNT_DATA.
ENDSELECT.
Regards,
Rich Heilman -
How to add a dynamic where clause for a sql based VO with group by query?
Hi,
Here is my case, I have a sql query based VO with the query like "select status, count(*) StatusCount from my_table group by status". Now I used the following java code trying to dynamically add the where clause to my VO to filter the rows based the type attribute in my DB table.
vo.setWhereClause("type='MyType1' ");
vo.executeQuery();
Then I got the sql syntax error. Looks like the ADF has added the where clause to the end of my sql so my sql becomes "select status, count(*) StatusCount from my_table group by status where type='MyType1' ". But what I expected was the correct syntax "select status, count(*) StatusCount from my_table where type='MyType1' group by status".
Does anyone know if this is an ADF bug? Or is there any other way to achieve my goal?
Thanks,
Chunyang
Edited by: Chunyang on Dec 13, 2012 9:09 PMHi,
When you use setWhereClause on the VO, it is applied on top of the VO query. I.e, assume your VO has the following query.
select empno, ename from empNow, if you apply the where clause programatically, only the two attributes that you are using in the select statement could be used. I.e
select * from (select empno, ename from emp) where ename='KING' - VALID
select * from (select empno, ename from emp) where deptno=10 - INVALID (because the inner query - the one you've defined as query for your vo does not have deptno attribute selected)If you would need to set a dynamic where clause, you need to make them available in your select statement / use bind variables.
-Arun -
Dynamic where clause in "delete itab where..." variant
Hi, I need to be able to delete the contents of an internal table based on a dynamic where clause. I don't want to loop through the entire table to test each record. Since the delete statement doesn't seem to allow a dynamic "where" clause I am trying to use a macro. I can't get the macro to work either.
I get the following error in my syntax check:
Statement concluding with "...FINAL_STATEMENT" ended unexpectedly.
Is there any way to get this macro to pass the syntax text or accomplish the dynamic delete?
MACRO TO FILTER OUT UNWANTED RECORDS (RETAIL OUTLETS)
DEFINE DELOUT.
DELETE MY_ITAB WHERE &1.
END-OF-DEFINITION.
Example FINAL_STATEMENT = ( ZRRDIVIS = '4' )
USING MACRO DOES NOT PASS SYNTAX CHECK
DELOUT FINAL_STATEMENT.Well, if you really need this kind of functionality, here is a code sample that I just threw together that would work. A lot of code for one little delete statement though.
report zrich_0001 .
data: begin of itab occurs 0,
fielda type c,
fieldb type c,
fieldc type c,
end of itab.
data: where_clause type string.
class lcl_source definition.
public section.
types: t_source(72).
class-data: routine(32) value 'TEMP_ROUTINE',
program(8),
message(128),
line type i.
class-data: isource type table of t_source,
xsource type t_source.
class-methods: execute_source importing im_clause type string.
endclass.
start-of-selection.
* Build the itab.
itab-fielda = '1'.
itab-fieldb = '2'.
itab-fieldc = '3'.
append itab.
itab-fielda = '3'.
itab-fieldb = '2'.
itab-fieldc = '1'.
append itab.
* Set the where clause
where_clause = 'FIELDA = ''1''.'.
* call the delete statement.
call method lcl_source=>execute_source
exporting
im_clause = where_clause.
* CLASS lcl_source IMPLEMENTATION
class lcl_source implementation.
method execute_source.
xsource = 'REPORT ZTEMP_REPORT.'.
insert xsource into isource index 1.
xsource = 'FORM & tables itab.'.
replace '&' with routine into xsource.
insert xsource into isource index 2.
xsource = 'Data: begin of xitab occurs 0,'.
append xsource to isource.
xsource = 'fielda type c,'.
append xsource to isource.
xsource = 'fieldb type c,'.
append xsource to isource.
xsource = 'fieldc type c,'.
append xsource to isource.
xsource = 'end of xitab.'.
append xsource to isource.
xsource = 'xitab[] = itab[].'.
append xsource to isource.
concatenate 'DELETE xITAB WHERE' im_clause into xsource
separated by space.
append xsource to isource.
xsource = 'ENDFORM.'.
append xsource to isource.
generate subroutine pool isource name program
message message
line line.
if sy-subrc = 0.
perform (routine) in program (program) tables itab.
else.
write:/ message.
endif.
endmethod.
endclass.
Regards,
Rich Heilman
Maybe you are looking for
-
Need help erase and install panther. system apps abnormality.
hello my ibook has been erratic recently. and now iTunes, activity monitor, grab, installer, disk utility... etc. do not launch (cannot open app because xxx is damaged or incomplete). i've tried fixing permissions, prebinding, emptying cache. i've co
-
I get the notice that there is a new update and if I want to restart Firefox. I click restart and then i get a message that the update fails and it will now download the entire file (?). I allow it to do this and then click restart again but get an e
-
CAN Workflow items in KM appear in the UWL
Hi We are looking the features of the Universal Worklist and have configured SAP Business Workflow to appear in the UWL. Can all your workflow items in KM (Document Management) be configured so that the end user and one place to see all workflow item
-
Messages are blocked in XI QUEUES
hi experts my scenario flow is r/3 ---> Xi - > again same R/3 data is going to XI automatically and in also from XI inbound QUeue but the problem is that it is showing Green flag if we relase the msgs maually from the queues then it showing Single bl
-
How to make a Business Package
Hello All I want to create Business Package of the application that I made in the CAF, that contains everything like Roles, Worksets, Pages, iViews. How can I make a Business Package out of that so that 3rd party can use it. When we deploy CAF projec