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

Select statement to execute dynamically

Former Member
0 Likes
1,466

Hi,

I just want to execute the Select statement dynamically using open cursor statement and without Open cursor statment (Just Select statement).....

How can i do that in the program...

data s_cursor type cursor.
data:it_mara TYPE TABLE OF mara,
     v_stmt type string.

* Case 1 - Open cursor
v_stmt = 'SELECT * FROM MARA.'.
OPEN CURSOR WITH HOLD s_cursor FOR 
  v_stmt . "How can i execute the Select statement here

DO.
  FETCH NEXT CURSOR s_cursor APPENDING TABLE it_mara.
  IF sy-subrc  <> 0.
    EXIT.
  ENDIF.
ENDDO.

* Case 2- No Open Cursor
v_stmt.

7 REPLIES 7
Read only

Former Member
0 Likes
1,101

Have you tried normal dynamic SELECTs here?

rob

Read only

0 Likes
1,101

I am sorry, I don't know... what is that means....

Read only

0 Likes
1,101

Check the ABAP documentation for SELECT and look at the columns and source areas. That shows you how to do exactly (I think) what you want. Get back to the forum if you have problems after reading that.

Rob

Read only

0 Likes
1,101

I used normal Select like the below. But i want to execute dynamic open SQL statments using Open Curosr, This is my requirement.

Like this OPEN CURSOR WITH HOLD s_cursor FOR (Open SQL statement).

data s_cursor type cursor.

data:it_mara TYPE TABLE OF mara.

  • v_stmt type string.

  • Case 1 - Open cursor

  • v_stmt = 'SELECT * FROM MARA.'.

OPEN CURSOR WITH HOLD s_cursor FOR

select * from mara.

DO.

FETCH NEXT CURSOR s_cursor APPENDING TABLE it_mara.

IF sy-subrc 0.

EXIT.

ENDIF.

ENDDO.

  • Case 2- No Open Cursor

select * from mara into table it_mara.

Read only

0 Likes
1,101

Hi fract_get,

I wonder why you want to use OPEN CURSOR at all because I can not imagine any requirement that makes it obligatory. You may correct me when I say that OPEN CURSOR and FETCH are from the very early times when the database interface could not really help you much.

Using an ECC600 system this is simply obsolete. There are better ways.

Please convince me there are good reasons to use OPEN CURSOR WITH HOLD. What are the 'dynamics'?

I'm always happy if I can learn something.

Regards,

Clemens

Read only

0 Likes
1,101

I am using OPEN CURSOR for downloading the table data in multiple files based on package size. I want to use Open sql statment as a input from file. Here is my requirement...How can i make it executable the Open SQL statements here OR Is there any other way.....

DATA: BEGIN OF  IT_OPEN_SQL OCCURS 0,
        QUERY TYPE STRING,
        END OF  IT_OPEN_SQL.
DATA: V_SIZE TYPE I.

LOOP AT IT_OPEN_SQL.
  OPEN CURSOR WITH HOLD C_CURSOR FOR
      (IT_OPEN_SQL-QUERY).
  DO.
    FETCH NEXT CURSOR C_CURSOR INTO TABLE <F_FS> PACKAGE SIZE V_SIZE.
    IF SY-SUBRC NE 0.
      CLOSE CURSOR C_CURSOR.
      EXIT.
    ENDIF.
* Downloading into file.......
  ENDDO.
ENDLOOP.

Read only

0 Likes
1,101

I solved my problem with the following link.