2012 May 23 9:06 AM
Hi,
My requirement specifies to fetch certain fields from a certain table at runtime.
The catch is, the fields to be fetched, the table from which fields to be fetched and the where conditions would be known only at runtime and in variables.
So i need something like this :
v_field1 = 'NAME1'.
v_tablename = 'KNA1'.
v_keyfield = 'KUNNR'.
select single (v_field1)
from (v_tablename)
into v_name1
where ( v_keyfield = p_kunnr ).
only problem i am facing is in the dynamic where clause , as its not accepting a variable in it. And throws a dump for dynamic declaration.
I have read a lot of discussions on dynamic internal table, but they don't really help.Even using field symbols to create dynamic ranges and assign in where clause doesn't seem to be a solution as i don't know the field names before hand.
Any Suggestions?
2012 May 23 9:19 AM
Hi,
Just try this. Maybe it will help.
DATA: v_field1 TYPE string,
v_tablename TYPE string,
v_keyfield TYPE string,
v_name1 TYPE kna1-name1,
g_kunnr TYPE kna1-kunnr.
PARAMETERS: p_kunnr LIKE g_kunnr.
v_field1 = 'NAME1'.
v_tablename = 'KNA1'.
v_keyfield = 'KUNNR EQ p_kunnr'. " serve as value of WHERE in your selection
SELECT SINGLE (v_field1)
FROM (v_tablename)
INTO v_name1
WHERE (v_keyfield).
IF sy-subrc EQ 0.
WRITE: v_name1.
ENDIF.
Regards,
Jake
2012 May 23 9:22 AM
Have you verified below posts.
http://scn.sap.com/message/13250122#13250122
http://wiki.sdn.sap.com/wiki/display/ABAP/Dynamic+where+clause
2012 May 23 9:25 AM
HI all,
Thanks, Got the answer in sap help itself...
v_field1 = 'NAME1'.
v_tablename = 'KNA1'.
v_keyfield = 'KUNNR'.
cond_Syntax = v_keyfield && '= p_kunnr'.
select single (v_field1)
from (v_tablename)
into v_name1
where (cond_syntax).
2012 May 23 9:36 AM
2012 May 23 9:28 AM
If you much confused in building a dyn where clause, the call
RH_DYNAMIC_WHERE_BUILD to build the condition.The table parameter where_clause return it yo you.
after that just call the select query for ex:
SELECT plvar otype objid from hrp1000 into it where ( dyn_cond ). Here dyn_cond is returned from the fm.
Donot forget to catch the exception cx_sy_dynamic_osql_semantics for the dynamic query used.
ex:
TRY .
SELECT plvar otype objid from hrp1000 into it where ( dyn_cond )
CATCH cx_sy_dynamic_osql_semantics INTO lref_err.
lv_msg = lref_err->get_text( ).
ENDTRY.