Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

How to execute a select query which is present in a string.

Former Member
0 Likes
840

Hi Experts,

I am having string variable. This String Variable is holding a simple SELECT QUERY.

For Example.

String qry.

qry = select * into i_table from vbak.

Now i want this query inside this string to be executed.

Is there any Functional Module or Is there any other way by which I can achieve this.

Thanks in Advance.

Aslam

Hi Experts,

I am having string variable. This String Variable is holding a simple SELECT QUERY.

For Example.

String qry.

qry = select * into i_table from vbak.

Now i want this query inside this string to be executed.

Is there any Functional Module or Is there any other way by which I can achieve this.

Thanks in Advance.

Aslam

5 REPLIES 5
Read only

Former Member
0 Likes
803

hi,

As of my knowledge you cannot execute the string which contains the select query...

Instead you can have the where clause in the variable.

select * into i_table from vbak where <var>.

Read only

0 Likes
803

you can execute select querry in string also.

Read only

Subhankar
Active Contributor
0 Likes
803

Hi...

You can do it other way. Please check the sample code.

TYPES : BEGIN OF x_marc,

matnr TYPE matnr,

werks TYPE werks_d,

END OF x_marc.

DATA: i_marc TYPE STANDARD TABLE OF x_marc,

l_marc TYPE char40 VALUE 'MARC'.

DATA: l_field TYPE STANDARD TABLE OF string,

l_where TYPE STANDARD TABLE OF string,

wa_field TYPE string.

PARAMETERS: p_matnr TYPE matnr.

wa_field = 'MATNR'.

APPEND wa_field TO l_field.

CLEAR: wa_field.

wa_field = 'WERKS'.

APPEND wa_field TO l_field.

CLEAR: wa_field.

wa_field = 'MATNR = P_MATNR'.

APPEND wa_field TO l_where.

SELECT (l_field) FROM (l_marc)

INTO TABLE i_marc WHERE (l_where).

IF sy-subrc = 0.

ENDIF.

You put the select part in a perform. Use can use it as function module.

Read only

Former Member
0 Likes
803

This is the code.

1. we can declare types dynamically

2. Internal table dynamically

3. Select querry dynamically

just copy paste this code

Here is the code for your question

DATA:v_fieldname

TYPE fieldname,

l_PROG TYPE string,

v_mess TYPE string,

l_sid TYPE string,

wa_ddfields TYPE dntab.

DATA i_tab TYPE STANDARD TABLE OF string.

DATA:l_str TYPE string,

l_str1 TYPE string.

PARAMETERS matnr type marc-matnr.

end-of-SELECTION.

*build the subroutine pool

APPEND 'PROGRAM subpool.' TO i_tab.

APPEND `LOAD-OF-PROGRAM.` TO i_tab.

APPEND `DATA i_tab1 TYPE TABLE OF vbak.` TO i_tab.

APPEND `DATA l_rows TYPE i.` TO i_tab.

APPEND `select * into table i_tab1 from vbak.` To i_tab.

append 'DESCRIBE TABLE i_tab1 LINES l_rows.' to i_tab.

append 'Write : l_rows .' to i_tab.

GENERATE SUBROUTINE POOL i_tab NAME l_PROG

MESSAGE v_mess

SHORTDUMP-ID l_sid.

IF sy-subrc = 0.

PERFORM ('LOOP_AT_TAB') IN PROGRAM (l_PROG) IF FOUND.

ELSEIF sy-subrc = 4.

MESSAGE v_mess TYPE 'I'.

ELSEIF sy-subrc = 8.

MESSAGE l_sid TYPE 'I'.

ENDIF.

Edited by: vijay wankhade on Jan 1, 2009 5:34 PM

Edited by: vijay wankhade on Jan 1, 2009 5:34 PM

Read only

former_member156446
Active Contributor
0 Likes
803

Hi Aslam

You can use parts of the string and use it some thing similar to below snippet:

PARAMETERS: carr_id TYPE spfli-carrid,
 conn_id TYPE spfli-connid.
 
DATA: where_clause TYPE STRING,
 and(4),
 wa_spfli TYPE spfli.
 
IF carr_id IS NOT INITIAL.
 CONCATENATE 'CARRID = ''' carr_id '''' INTO where_clause.
 and = ' AND'.
ENDIF.
IF conn_id IS NOT INITIAL.
 CONCATENATE where_clause and ' CONNID = ''' conn_id ''''
 INTO where_clause.
ENDIF.
SELECT * FROM spfli INTO wa_spfli WHERE (where_clause).
 WRITE: / wa_spfli-carrid, wa_spfli-connid, wa_spfli-cityfrom,
 wa_spfli-cityto, wa_spfli-deptime.
ENDSELECT.