Use of sap abap cursor (example)

Source: Internet
Author: User

Article from http://blog.csdn.net/szlaptop/article/details/8565285

  1. Report z_wuwei_0019.
  2. */Declaring CURSORS-C1 and C2
  3. Data: C1 type cursor,
  4. C2 type cursor.
  5. Data: work_area1 type Mara,
  6. Work_area2 type Mara.
  7. Data: flag1 (1) type C,
  8. Flag2 (1) type C.
  9. */Opening CURSORS-C1 and C2
  10. Open cursor: C1 for select matnr "item No.
  11. Ersda "creation date
  12. Ernam "Name of the person who created the object
  13. From Mara "General material data
  14. Where ernam = 'hangzhou ',
  15. C2 for select laeda "Last modified Date
  16. Aenam "Change Object User Name
  17. From Mara
  18. Where ernam = 'hangzhou '.
  19. Do.
  20. If flag1 ne 'x '.
  21. */Using the C1 cursor to fetch the data
  22. Fetch next cursor C1 into corresponding fields of work_area1.
  23. If SY-SUBRC <> 0.
  24. */Closing the C1 cursor
  25. Close cursor C1.
  26. Flag1 = 'x '.
  27. Else.
  28. Write:/'item: 'color 7, WORK_AREA1-MATNR, 'create Date: 'color 6, WORK_AREA1-ERSDA,' name of the person who created the object: 'color 5, WORK_AREA1-ERNAM.
  29. Endif.
  30. Endif.
  31. If flag2 ne 'x '.
  32. */Using the C2 cursor to fetch the data
  33. Fetch next cursor C2 into corresponding fields of work_area2.
  34. If SY-SUBRC <> 0.
  35. */Closing the C2 cursor
  36. Close cursor C2.
  37. Flag2 = 'x '.
  38. Else.
  39. Write:/'last modified Date: 'color 4, WORK_AREA2-LAEDA, 'change Object User name: 'color 3, WORK_AREA2-AENAM.
  40. Endif.
  41. Endif.
  42. If flag1 = 'X' and flag2 = 'x '.
  43. Exit.
  44. Endif.
  45. Enddo.

 

 

 

 

Article from http://blog.csdn.net/wengyupeng/article/details/6302261

Three examples of BC-ABAP programming are as follows:

1.

Data: C1 type cursor,
C2 type cursor.
Data: wa1 type spfli,
Wa2 type spfli.
Data: flag1,
Flag2.
Open cursor: C1 for select carrid connid
From spfli
Where carrid = 'lh ',
C2 for select carrid connid cityfrom cityto
From spfli
Where carrid = 'az '.
Do.
If flag1 ne 'x '.
Fetch next cursor C1 into corresponding fields of wa1.
If SY-SUBRC <> 0.
Close cursor C1.
Flag1 = 'x '.
Else.
Write:/WA1-CARRID, WA1-CONNID.
Endif.
Endif.
If flag2 ne 'x '.
Fetch next cursor C2 into corresponding fields of wa2.
If SY-SUBRC <> 0.
Close cursor C2.
Flag2 = 'x '.
Else.
Write:/WA2-CARRID, WA2-CONNID,
WA2-CITYFROM.
Endif.
Endif.
If flag1 = 'X' and flag2 = 'x '.
Exit.
Endif.
Enddo.

2.

Data C type cursor.
Data WA type sbook.
Open cursor C for select carrid connid fldate bookid smoker
From sbook
Order by carrid connid fldate smoker bookid.
Fetch next cursor C into corresponding fields of WA.
While SY-SUBRC = 0.
If WA-SMOKER = ''.
Perform nonsmoker using C.
Elseif WA-SMOKER = 'x '.
Perform smoker using C.
Skip.
Else.
Exit.
Endif.
Endwhile.
Form nonsmoker using n_cur type cursor.
While WA-SMOKER = ''and SY-SUBRC = 0.
Format color = 5.
Write:/WA-CARRID, WA-CONNID, WA-FLDATE, WA-BOOKID.
Fetch next cursor n_cur into corresponding fields of WA.
Endwhile.
Endform.
Form smoker using s_cur type cursor.
While WA-SMOKER = 'X' and SY-SUBRC = 0.
Format color = 6.
Write:/WA-CARRID, WA-CONNID, WA-FLDATE, WA-BOOKID.
Fetch next cursor s_cur into corresponding fields of WA.
Endwhile.
Endform.

The program opens a cursor for the database table sbook. After the first fetch
Statement, a subroutine is called, which is dependent on the contents of
Smoker column. The cursor is passed to an interface parameter in the subroutine.
The subroutines read further lines until the contents of the smoker column change.
The subroutines perform different tasks using the lines read by the cursor.

3.

Data: wa_spfli type spfli,
Wa_sflight type sflight.
Data: C1 type cursor,
C2 type cursor.
Open cursor C1 for select *
From spfli
Order by primary key.

Open cursor C2 for select *
From sflight
Order by primary key.
Do.
Fetch next cursor C1 into wa_spfli.
If SY-SUBRC Ne 0.
Exit.
Endif.
Write:/WA_SPFLI-CARRID, WA_SPFLI-CONNID.
Do.
Fetch next cursor C2 into wa_sflight.
If SY-SUBRC <> 0 or WA_SFLIGHT-CARRID <> WA_SPFLI-CARRID
Or WA_SFLIGHT-CONNID <> WA_SPFLI-CONNID.
Exit.
Else.
Write:/WA_SFLIGHT-CARRID, WA_SFLIGHT-CONNID,
WA_SFLIGHT-FLDATE.
Endif.
Enddo.

The program opens a cursor for each of the table spfli and sflight. Since both
Tables are linked by a foreign key relationship, it is possible to program a nested loop
By sorting the selection by its primary key, so that the data read in the Inner Loop
Depends on the data in the outer loop.
This programming method is quicker
Using nested select statements, since the cursor for the inner loop does not
Continually have to be reopened.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.