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 this SQL Query in ABAP Program.

Former Member
0 Likes
3,436

Hi,

I have a string which is the SQL Query.

How to execute this sql Query (SQL_STR) in ABAP Program.

Code:-

DATA: SQL_STR type string.

SQL_STR = 'select * from spfli.'.

Thanks in Advance,

Vinay

1 ACCEPTED SOLUTION
Read only

nablan_umar
Product and Topic Expert
Product and Topic Expert
0 Likes
1,936

Hi Vinay,

You can't execute this string of Abap statement in Abap.

You can partially have a dynamic statement within your SELECT statement. eq.

SELECT (ftab) from (tabname).

where ftab is an internal table of string which contains '*' and tabname is a string which is the table name 'SPFLI'.

You also can create a form pool that contains the source code of a subroutine which 'SELECT * FROM SPFLI' is one of the line.

Message was edited by: Nablan Umar

5 REPLIES 5
Read only

Former Member
0 Likes
1,936

Hi there,

Looks like you wish to run some Native SQL - am I right ?

Try using EXEC SQL syntax.

Here is an example :

Example

Displaying an extract from the table AVERI_CLNT:

DATA: F1(3), F2(3), F3(3).

F3 = ' 1 '.

EXEC SQL.

SELECT CLIENT, ARG1 INTO :F1, :F2 FROM AVERI_CLNT

WHERE ARG2 = :F3

ENDEXEC.

WRITE: / F1, F2.

Hope this helps.

If so, please award me the points as so few people do.

Cheers

Colin. )

Read only

nablan_umar
Product and Topic Expert
Product and Topic Expert
0 Likes
1,937

Hi Vinay,

You can't execute this string of Abap statement in Abap.

You can partially have a dynamic statement within your SELECT statement. eq.

SELECT (ftab) from (tabname).

where ftab is an internal table of string which contains '*' and tabname is a string which is the table name 'SPFLI'.

You also can create a form pool that contains the source code of a subroutine which 'SELECT * FROM SPFLI' is one of the line.

Message was edited by: Nablan Umar

Read only

ssimsekler
Product and Topic Expert
Product and Topic Expert
0 Likes
1,936

Hi Vinay

Here is a sample to dynamically generate a subroutine-pool having your SQL and calling it.

REPORT dynamic_sql_example .

DATA: BEGIN OF gt_itab OCCURS 1 ,

line(80) TYPE c ,

END OF gt_itab .

DATA gt_restab TYPE .... .

DATA gv_name(30) TYPE c .

DATA gv_err(120) TYPE c .

START-OF-SELECTION .

gt_itab-line = 'REPORT generated_sql .' .

APPEND gt_itab .

gt_itab-line = 'FORM exec_sql CHANGING et_table . ' .

APPEND gt_itab .

gt_itab-line = SQL_STR .

APPEND gt_itab .

gt_itab-line = 'ENDFORM.' .

APPEND gt_itab .

GENERATE SUBROUTINE POOL gt_itab NAME gv_name MESSAGE gv_err .

PERFORM exec_sql IN PROGRAM (gv_name) CHANGING gt_restab

IF FOUND .

WRITE:/ gv_err .

LOOP AT gt_result .

WRITE:/ .... .

ENDLOOP .

*--Serdar

Read only

0 Likes
1,936

Hi Serdar Simsekler ,

Your code really helped me,

Thanks & Regards

Vinay

Read only

0 Likes
1,936

Hi Vinay,

I have similar requirement for dynamic sql query. Can you provide complete code for this on sdn.

Thanks & Regards

R. Singh