‎2004 Oct 07 2:24 PM
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
‎2004 Oct 07 3:54 PM
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
‎2004 Oct 07 3:52 PM
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. )
‎2004 Oct 07 3:54 PM
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
‎2004 Oct 07 8:59 PM
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
‎2004 Oct 08 11:45 AM
Hi Serdar Simsekler ,
Your code really helped me,
Thanks & Regards
Vinay
‎2011 Mar 03 7:06 AM
Hi Vinay,
I have similar requirement for dynamic sql query. Can you provide complete code for this on sdn.
Thanks & Regards
R. Singh