Dump using Dynamic WHERE condition (EXISTS + subquery)
Hi experts,
I want to use dynamic WHERE here, but I got a problem when using EXISTS + subuery.
Here is my code snippet:
And the actual SQL sentence should be like this:
When I execute this program, an exception CX_SY_DYNAMIC_OSQL_SEMANTICS is raised and the program dumps.
Dump Information:
Short text
A dynamically specified column name is unknown.
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 "FRM_GET_ALL_PROD_ORDERS" "(FORM)", 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:
An Open SQL clause was specified dynamically. The contained field name
"EXISTS" does not exist in any of the database tables from the FROM clause.
I'm confused because the plain text of my WHERE condition looks good. Could you tell me why I encountered this problem?
Many thanks,
Shelwin
Hi Shelwin !
Testing your code i dont have dump, using this:
REPORT z_test MESSAGE-ID z_test_msgs.
DATA: lt_where_tab1 TYPE STANDARD TABLE OF edpline WITH HEADER LINE,
lt_where_tab2 TYPE STANDARD TABLE OF edpline WITH HEADER LINE,
gt_detail TYPE STANDARD TABLE OF caufv WITH HEADER LINE.
CONCATENATE 'EXISTS (SELECT J_1~STAT FROM JEST AS J_1 INNER JOIN TJ02T AS T_1'
'ON J_1~STAT EQ T_1~ISTAT'
'WHERE J_1~OBJNR EQ CAUFV~OBJNR'
'AND J_1~INACT EQ '' '''
'AND T_1~SPRAS EQ ''E'''
'AND T_1~TXT04 EQ ''CRTD'')'
INTO lt_where_tab1.
CONCATENATE 'NOT EXISTS (SELECT J_2~STAT FROM JEST AS J_2 INNER JOIN TJ02T AS T_2'
'ON J_2~STAT EQ T_2~ISTAT'
'WHERE J_2~OBJNR EQ CAUFV~OBJNR'
'AND J_2~INACT EQ '' '''
'AND T_2~SPRAS EQ ''E'''
'AND T_1~TXT04 IN (''TECO'' , ''DLFL'', ''DLV'', ''CLSD''))'
INTO lt_where_tab2.
SELECT aufnr ftrms gltrs
FROM caufv
INTO CORRESPONDING FIELDS OF TABLE gt_detail
WHERE (lt_where_tab1)
AND (lt_where_tab2).
BREAK-POINT.
Regards,
Edited for error on image.
Similar Messages
-
Single quote in dynamic where condition
BAPI_STUDENT_IDENTIFIC_ADD has a field called IDENTIFICATIONNUMBER. This field is later used in a dynamic where condition and causes a short dump when it contains a single quote. I will change the program that calls the BAPI to check for single quotes, but is there anything else I need to check for to ensure a correct where condition?
Thanks,
DanHi Dan,
The best way to ensure correctness of syntax of a dynamic where condition is:
Run the program in debugging mode, Get the Query that is generated dynamically and write it to some other ABAP program and perform syntax check.
This will help you to remove all the syntax errors.
Regards,
Darshil -
Dynamic select query with dynamic where condition
Hi all,
I want to use the dynamic select query with dynamic where condition. For that I used the below code but I am getting dump when using this code.
Please advice, if there is any other way to achieve this requirement.
Thanks,
Sanket Sethi
Code***************
PARAMETERS: p_tabnam TYPE tabname,
p_selfl1 TYPE edpline,
p_value TYPE edpline,
p_where1 TYPE edpline .
DATA: lt_where TYPE TABLE OF edpline,
lt_sel_list TYPE TABLE OF edpline,
l_wa_name TYPE string,
ls_where TYPE edpline,
l_having TYPE string,
dref TYPE REF TO data,
itab_type TYPE REF TO cl_abap_tabledescr,
struct_type TYPE REF TO cl_abap_structdescr,
elem_type TYPE REF TO cl_abap_elemdescr,
comp_tab TYPE cl_abap_structdescr=>component_table,
comp_fld TYPE cl_abap_structdescr=>component.
TYPES: f_count TYPE i.
FIELD-SYMBOLS : <lt_outtab> TYPE ANY TABLE,
* <ls_outtab> TYPE ANY,
<l_fld> TYPE ANY.
struct_type ?= cl_abap_typedescr=>describe_by_name( p_tabnam ).
elem_type ?= cl_abap_elemdescr=>describe_by_name( 'F_COUNT' ).
comp_tab = struct_type->get_components( ).
comp_fld-name = 'F_COUNT'.
comp_fld-type = elem_type.
APPEND comp_fld TO comp_tab.
struct_type = cl_abap_structdescr=>create( comp_tab ).
itab_type = cl_abap_tabledescr=>create( struct_type ).
l_wa_name = 'l_WA'.
CREATE DATA dref TYPE HANDLE itab_type.
ASSIGN dref->* TO <lt_outtab>.
*CREATE DATA dref TYPE HANDLE struct_type.
*ASSIGN dref->* TO <ls_outtab>.
* Creation of the selection fields
APPEND p_selfl1 TO lt_sel_list.
APPEND 'COUNT(*) AS F_COUNT' TO lt_sel_list.
** Creation of the "where" clause
*CONCATENATE p_selfl1 '= '' p_value ''.'
* INTO ls_where
* SEPARATED BY space.
*APPEND ls_where TO lt_where.
* Creation of the "where" clause
APPEND p_where1 TO lt_where.
* Creation of the "having" clause
l_having = 'count(*) >= 1'.
* THE dynamic select
SELECT (lt_sel_list)
FROM (p_tabnam)
INTO CORRESPONDING FIELDS OF TABLE <lt_outtab>.
* WHERE (lt_where).Hi Sanket,
The above given logic of mine works for you, put the code in the If condition and try-
just like below:
IF NOT P_EBELN IS INITIAL.
lt_where = '& = ''&'' '.
REPLACE '&' WITH p_ebeln INTO lt_where.
REPLACE '&' WITH field_value INTO lt_where.
SELECT (lt_sel_list) INTO CORRESPONDING FIELDS OF TABLE <lt_outtab>
FROM (p_tabnam)
WHERE (lt_where).
ENDIF.
thanks\
Mahesh -
Hi, I'm building a procedure with 4 parameters το pass in input and I ask if it's possible to build a select inside the procedure with a dynamic where condition.
The query with 2 conditions is the following:
SELECT count(incident_number) FROM cs.cs_incidents_all_b
1st condition
WHERE (TO_DATE(incident_attribute_6,'dd-mm-yyyy hh24:mi') BETWEEN TO_DATE(p_create_date_ll,'dd-mm-yyyy hh24:mi') AND TO_DATE(p_create_date_ul,'dd-mm-yyyy hh24:mi'))
2nd condition
AND external_attribute_4 BETWEEN p_resolv_time_ll AND p_resolv_time_ul;
The first condition has to be always valid, but the second one, depending on the parameters has to be satisfied or not.
The first 2 parameters (p_create_date_ll and p_create_date_ul) are always written as a string (so the first condition always works).
The second pair of paramenters (p_resolv_time_ll and p_resolv_time_ul) can be written as numbers (from a form), or written both as '*'
So, If the parameters p_resolv_time_ll and p_resolv_time_ul are numbers, I should retrieve a small set of data (all the 2 where condition have to work),
while if they are both '*' ONLY the first condition has to work. Practically, in this last case the 2nd condition is how it was commented.
Is it possible to build something that satisfy my request ?
Thanks in advance
AlexYes I know....
If I use an IF THEN ELSE statement I can write 2 different queries with 2 different where conditions....but I want to avoid this solution because I have 4 pair of these parameters which can assume differents values and depending on their values I have to use different where conditions
If I use an IF THEN ELSE statement, this means to write 16 different queries....
I already used DECODE or CASE but they not satisfy my request....
Anybody has others ideas ?
Thanks -
Getting an error saying no primary index is used in where condition in SCI.
Hi,
I have develped a report using table PAYR.
The selection -screen of this report consists of Runid, Identification and Payment date.
The select qurey i used is
SELECT chect
zaldt
rwbtr
znme1
voidd
FROM payr INTO TABLE it_details
WHERE laufd EQ p_laufd AND
laufi EQ p_laufi AND
zaldt EQ p_date.
But when i check the code with code inspector im getting an error saying no primary index is used in where condition.
How can i solve this problem.
Pls. provide some pointers.
Thanks.HI Deepti,
The problem is since the fields used in where condition is not mentioned in any of the indexes created for table ( you can check the maintained indexes for table by clicking on the 'Indexes' button or ctrl+F5)
Atlease include MANDT in where condition, or create your own index with the fields which you have used in SELECT query.
this will solve the problem
Regards
Seema -
Issue with Dynamic WHERE condition in Cursor in FUNCTION.
Hi All,
I am facing an issue with cursor having dynamic WHERE condition in a function.
Below is the FUNCTION:
CREATE OR REPLACE FUNCTION EXCEPTION_MERGE(TABLE_NAME IN VARCHAR2, TAB_NAME IN VARCHAR2)
RETURN VARCHAr2
IS
stmt_tabcols VARCHAR2(32767);
v_columnname VARCHAR2(32767);
CURSOR C1 IS
SELECT 'A.'||A.COLUMN_NAME ||' = '|| 'B.'||B.COLUMN_NAME COLUMN_NAME
FROM
SELECT COLUMN_ID, COLUMN_NAME
FROM USER_TAB_COLUMNS
WHERE TABLE_NAME = TABLE_NAME
AND COLUMN_NAME NOT IN ('ERROR_TAB_ID','ERROR_LOAD_DATE')
) A,
SELECT COLUMN_ID, COLUMN_NAME
FROM USER_TAB_COLUMNS
WHERE TABLE_NAME = TAB_NAME
) B
WHERE A.COLUMN_ID = B.COLUMN_ID;
BEGIN
FOR TABCOL IN C1
LOOP
stmt_tabcols := stmt_tabcols ||TABCOL.COLUMN_NAME||',';
END LOOP;
stmt_tabcols := RTRIM(stmt_tabcols, ',');
RETURN stmt_tabcols;
END;
SELECT EXCEPTION_MERGE('WC_W_TEST_FS','WC_W_TEST_FS_GBL') FROM DUAL;It throws, below error:
ORA-06502 : PL/SQL : Numeric or value error : character string buffer too smallIf I REPLACE TABLE_NAME and TAB_NAME with hard coded values , it works fine. Can somebody look at the code and let me know the issue.
Edited by: ace_friends22 on Sep 9, 2012 1:08 PMEtbin neatly demonstrating the value of posting code in a manner which makes it easy to read.
It's obviously an naming/scoping issue. Faced with a join like this:
where table_name = table_namethe engine looks for something called table_name in the current scope. It finds it, a column on USER_TAB_COLUMNS and applies it to both sides of the filter. It has no way of knowing that there is also a parameter called TABLE_NAME, because that is outside its current scope. Consequently the query will join every table in your schema regardless of what values you pass, and that's why you blow the buffer.
Takw etbin's advice and name your parameter with a prefix:
where table_name = p_table_nameThis isn't a column in USER_TAB_COLUMNS which will force the engine to look in the next scope up, which in your case is the function, where it will find your parameter and so generate a query for the passed values only.
Cheers, APC
Edited by: APC on Sep 9, 2012 8:03 AM -
Using 'Greater than/less than' in dynamic where condition
Hi Guys,
I am trying to use a logical expression with GT/LT condition in dynamic where, butits giving dump "expression worngly parenthesised' . My query runs perfectly fine when the logical expression does not have 'GT/LT' . Is there a different way of using GT/LT in dynamic where or is it not posiible at all?? Please find my query below -
IF vendor IS NOT INITIAL.
i_where_clause = 'a~lifnr = vendor'.
APPEND i_where_clause.
CLEAR i_where_clause.
ENDIF.
IF to_date IS NOT INITIAL.
i_where_clause = 'a~zzearliestdep LT to_date'.
APPEND i_where_clause.
CLEAR i_where_clause.
ENDIF.
IF from_date IS NOT INITIAL.
i_where_clause = 'a~zzlatestdep GT from_date'.
APPEND i_where_clause.
CLEAR i_where_clause.
ENDIF.
SELECT aebeln alifnr azzearliestdep azzlatestdep
bebelp belikz b~kzabs
INTO CORRESPONDING FIELDS OF TABLE i_podetails
UP TO max_hits ROWS
FROM ( ekko AS a INNER JOIN ekpo AS b
ON aebeln = bebeln )
WHERE a~ebeln = ebeln AND
(i_where_clause).Hi,
Try this -
IF vendor IS NOT INITIAL.
i_where_clause = 'a~lifnr = vendor'.
APPEND i_where_clause.
ENDIF.
IF to_date IS NOT INITIAL.
if i_where_cause[] is not initial.
i_where_clause = 'AND'
append i_where_cause.
endif.
i_where_clause = 'a~zzearliestdep LT to_date'.
APPEND i_where_clause.
ENDIF.
IF from_date IS NOT INITIAL.
if i_where_cause[] is not initial.
i_where_clause = 'AND'
append i_where_cause.
endif.
i_where_clause = 'a~zzlatestdep GT from_date'.
APPEND i_where_clause.
CLEAR i_where_clause.
ENDIF.
Cheers.
\[removed by moderator\]
Edited by: Jan Stallkamp on Jun 26, 2008 1:08 PM -
Dynamis where Condition using select-options
Hi All,
I am trying to form a dynamic where clause using values entered on a selection screen. I want that if a new entry is added in the selection screen the where clause can also be modified dynamically without any change in coding. The only change required should be addition of select-option statement in the selection screen declaration.
Thanks in advance.
Anjurefer below example
L_COND_SYNTAX = 'd2ext_status = l_status and d2ext_date ne c_intdate and '
& 'd2~ext_uploaded = space'.
SELECT D1TKNUM D1SHTYP D1TPLST D1ERNAM D1ERDAT D1BFART
D1VSART D1ROUTE D1SIGNI D1EXTI1 D1~TPBEZ
D1DATBG D1UATBG D1TDLNR D2VBELN D2~POSNR
D2EXT_FAILED D2EXT_COUNT D2EXT_DATE D2EXT_TIME
D2EXT_REP_DATE D2EXT_REP_TIME
D2EXT_RES_DATE D2EXT_RES_TIME D2~EXT_ERR_TYPE
INTO TABLE PA_GT_SHIP_HEADER
FROM VTTK AS D1 INNER JOIN RIOZTM_DELSTAT AS D2
ON D1TKNUM = D2TKNUM
WHERE
D1~TKNUM IN S_TKNUM AND
D1~SHTYP IN S_SHTYP AND
AND
<b>(L_COND_SYNTAX).</b>
Plz note the backet in L_COND_SYNTAX.
Reward points if helpful -
Dynamic where condition - Problem
Hi All,
I have a problem in a select statement where I use a dynamic condition.It gives me a dump
SAPSQL_WHERE_PARENTHESES
CX_SY_DYNAMIC_OSQL_SYNTAX.
Please find the below code which I implemented.
*SELECT * FROM ZXR5*
WHERE (SOURCE).
The value of SOURCE is 'MATNR EQ '000000000000200066'.
When I checked the help, it is advised to use a CATCH exception which I did as below:
TRY.
*SELECT * FROM ZXR5*
WHERE (SOURCE).
CATCH cx_sy_dynamic_osql_error.
MESSAGE `Wrong WHERE condition!` TYPE 'I'.
ENDTRY.
Now, the select statement gives an exception and is not executing to fetch the values.
I need some input on the above issue. Any help on this would be greatly appreciated.
Regards,
Rajmohamed.MHi Vijay,
I tried the same and all the time, the select statement gives me an exception and wont proceed.
TRY.
SELECT * FROM ZXR5
INTO TABLE <L_ZXR5>
WHERE (SOURCE).
CATCH cx_sy_dynamic_osql_error.
MESSAGE `Wrong WHERE condition!` TYPE 'I'.
ENDTRY.
Regards,
Raj -
How to execute procedure with dynamic where condition.
Hi All,
I am facing a probelem
Issue :
I am having a parameter p_id in my procedure based on the value of this parameter i want to change the where condition.
How to do so?
Code:---------
create or replace procedure p_1
( p_id in emp_dummy.empno%type)
is
parameter_1 varchar2(2000);
v_sal number;
begin
if p_id = 1 then
parameter_1 := 'where empno = 1';
else
parameter_1 := 'where 1=1';
end if;
select salary into v_sal from emp_dummy||' '||parameter_1;
end;Dynamic SQL is not the best of ideas, but if you must...
EXECUTE IMMEDIATE 'select salary from emp_dummy where empno=nvl(:x,empno)' into v_sal using p_id;Then if you pass in the p_id to the procedure it will select the salary for that employee and if null is passed in then it will select the salary for all employees. Now here's a problem because you are trying to return a single value and if you don't specify the employee then it will try and return multiple values from that select, so you probably want to do something like give the sum of the salaries...
EXECUTE IMMEDIATE 'select sum(salary) from emp_dummy where empno=nvl(:x,empno)' into v_sal using p_id; -
REP 300 error when using dynamic where clause
Hi,
I am developing a report and I need a dynamic where clause.The code I have written in the where clause is as below
function where_clauseFormula return VARCHAR2 is
begin
if :P_PO_REQD = 'No' then
return ('and pha.segment1 is null');
elsif :P_PO_REQD = 'Yes' then
return('and pha.segment1 between NVL(:p_po_num_from,1) and NVL(:p_po_num_to,99999999999999999)');
else return NULL;
end if;
end;
Now if i directly use either of the conditions in my report query it works fine.But if the parameter is No then the where clause does not work.I have also tried different statements in the if clause of the 'No' part and they work.For example the following code works when :P_PO_REQD is 'No' :
function where_clauseFormula return VARCHAR2 is
begin
if :P_PO_REQD = 'No' then
return ('and pha.segment1 between NVL(:p_po_num_from,1) and NVL(:p_po_num_to,99999999999999999)');
elsif :P_PO_REQD = 'Yes' then
return('and pha.segment1 between NVL(:p_po_num_from,1) and NVL(:p_po_num_to,99999999999999999)');
else return NULL;
end if;
end;
I don't know why this is happening.I really need a solution or a workaround urgently
Thanks
MukundYou may use Lexical Reference for the dynamic where. In After Parameter Form Trigger, code....
if :P_PO_REQD = 'No' then
:P_where := :P_where || 'and pha.segment1 is null';
elsif :P_PO_REQD = 'Yes' then
:P_where := :P_where || 'and pha.segment1 between NVL(:p_po_num_from,1) and NVL(:p_po_num_to,99999999999999999)';
end if;
.......... -
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). -
SELECT ... WHERE (codsyntax).
the consyntax should have one or combination of the value
1) txps = 'a~pspid = waparm-pspid'.
2) txpo = 'b~posid = waparm-posid'.
3) txau = 'c~aufnr = waparm-aufnr'.
the input options:
1) codsyxtax = txps
2) codsyxtax = txpo
3) codsyxtax = txau
4) codsyxtax = txps and txpo
5) codsyxtax = txps and txau
6) codsyxtax = txpo and txau
7) codsyxtax = txps and txpo and txau
i'm tried to something like this, just i think is quite messy. thanks you
if waparm-pspid is not initial and waparm-posid is initial.
concatenate whercond txps into whercond separated by space.
elseif waparm-pspid is not initial and waparm-posid is not initial.
concatenate whercond txps into whercond separated by space.
concatenate whercond 'and' into whercond separated by space.
endif.hi augustarian,
i'll store this into an internal table. from the data. it can be seen there a BLANK input. so the blank input should not be included into WHERE connditions. i tried to do something like this:
PSPID POSID AUFNR PSY PUS cond_syx
aa-aa-aa a1 I001 if proj~pspid = itparm-pspid and prps~posid = itparm~posid and ....
bb-bb-bb b1 E009 if proj~pspid = itparm-pspid and aufk~aufnr = itparm~paufnr and ....
cc-cc-cc if proj~pspid = itparm-pspid
and implement with FOR ALL ENTERIES, but at the WHERE conditions i failed to put the CONF_SYX. please comment and give opinions. Thanks you very much. -
Subquery in dynamic where-clause
Hi,
i'm trying to build a dynamic sql-statement in OPEN SQL, which uses a subquery in the dynamic where-clause.
In the following example code the first select works fine, the second one raises an error.
report zerrorforum.
data:
l_var_anzahl
* No Error
select
count(*)
into
l_var_anzahl
from
t000 as t1
where
t1~mandt in ( select t2~mandt from t000 as t2 where t2~mandt = t1~mandt ).
* Error
select
count(*)
into
l_var_anzahl
from
t000 as t1
where
('T1~MANDT IN ( SELECT T2~MANDT FROM T000 AS T2 WHERE T2~MANDT = T1~MANDT )').
The Error Analysis says the following:
The exception, which is assigned to class 'CX_SY_DYNAMIC_OSQL_SEMANTICS', was
not caught and therefore caused a runtime error. The reason for the exception is:
The current ABAP program has tried to execute an Open SQL statement
which contains a WHERE, ON or HAVING condition with a dynamic part.
The part of the WHERE, ON or HAVING condition specified at runtime in
a field or an internal table, contains the invalid value "SELECT".
Is this behaviour documented anywhere? I didn´t find anything.
Or is it a bug?
Klaus-Dieter LueppensHi,
it's not possible. Here is a quote from ABAP documentation from dynamic WHERE condition.
A logical expression can be specified as a parenthesized data object cond_syntax that contains the syntax of a logical expression or is initial when the statement is executed. It has been possible since SAP Web AS 6.40 to specify all logical expressions dynamically, with the exception of the evaluation of a subquery.
Cheers -
Dynamic component in WHERE-condition for LOOP AT ?
i want to make
loop at itab where (dynamic field) > 0
how i can make it...
Edited by: Thomas Zloch on Mar 2, 2010 2:47 PMI thought it's funny.
And the problem can maybe be solved by using a completely dynamic where-condition, not just a dynamic component.
http://help.sap.com/abapdocu_70/en/ABAPLOOP_AT_ITAB_COND.htm#!ABAP_ADDITION_4@4@
Thomas
P.S. meaningless subject enhanced, and please take the time to say hi and please when asking for help. Thanks.
Maybe you are looking for
-
How do I find a list of internet plugins
how do I find a list of internet plugings on my imac
-
Why are some channels offered on Select TV service not offered on Premium?
We get the Premium TV Service (we're in Maryland). The lower-priced Select service offers a number of channels that are not offered on Premium (but are offered on the higher priced plans). We're particulary interested in the BBC chanels, but there
-
Logic 10.1 for a specific project; there is no mixer and the display is completely erratic. For other projects it's working for now. Please help
-
Reg: WebI Report Failed to Excute with UnKnown Error
Dear Expert , I have Implemented , BOE With SAP Integration , Im able to Create the Universe. Using the DataSource , I have Created one WebIntelligence Report , Unfortunatly Im getting the Following Below Mnetioned Error. If I run the Query as Single
-
How do i install an opperating system on the hard disk?
product name hp pavillion product number a6z59ua