[Sap] dynamic generation of dynamic SQL excute programs for classic applications using ABAP

Source: Internet
Author: User

Development Description: during SAP system maintenance, sometimes we need to modify some table data, but many tables cannot be directly modified in tcode: SE16, the sap id used does not have the permission to modify the debugging data. How can we modify the data? Idea --> ABAP Program All SQL update statements in are permitted to be executed, as long as we can dynamically generate the ABAP code to modify the table field for dynamic execution! Development Technology: 1. SQL Code Writing Technology 1. dynamic program code generation technology 2. considerations for ABAP dynamic program execution: the SQL syntax must be accurate and the modification conditions must be accurate. The program code does not violate the rule of Data uniqueness after data modification: the following report is z_cbo_abap_02 message-id zp no standard page heading. data: fcode like sy-ucomm,
Changed like s38e-buf_varied,
Save_tabix like sy-tabix,
Tabix_count type I,
Select_key (10) type C,
Etc (80) type C,
Update_flag type C,
Line_cnt type I,
Prog (8) type C,
MSG (120) type C,
Msg_text (72) type C,
Confirm_flag Type C. Data: itab_ SQL like abapsource occurs 0 with header line,
Itab_prog like abapsource occurs 0 with header line. start-of-selection. * program execution directly enters the ABAP code editor
Set PF-STATUS 'pfsta00 '.
Write:/1 'edit your SQL...
If sy-ucomm = 'edit '.
Perform editor_ SQL. * The dynamically generated program executes elseif sy-ucomm = 'exec 'or sy-ucomm = 'edex '.
Refresh itab_prog.
Clear itab_prog.
If update_flag = 'x '.
Perform exec_modify.
Endif.
Endif .*&------------------------------------------------------------------*
* & Form editor_ SQL
*&------------------------------------------------------------------*
Form editor_ SQL.
* Call Editor
Call function 'editor _ application'
Exporting
Application = 'bf'
Display =''
Name = '[edit your SQL...]'
Importing
Fcode = fcode
Changed = changed
Tables
Content = itab_ SQL.
* Translate code upper
Loop at itab_ SQL.
Save_tabix = sy-tabix.
Tabix_count = tabix_count + 1.
If itab_sql-line = space or itab_sql-line + (1) = '*'.
Delete itab_ SQL index save_tabix.
Endif.
Translate itab_sql-line to upper case.
Modify itab_ SQL index save_tabix.
Endloop. * parsing input SQL code
Loop at itab_ SQL.
If sy-tabix = 1.
Shift itab_sql-line Left deleting leading space.
Endif.
Save_tabix = sy-tabix + 1.
Split itab_sql-line at space into select_key etc.
If select_key = 'select '.
Message i433 with 'donot support select syntax! ^ ~ ^ '.
Stop.
* Hehe ~~ Don't bother myself.
Elseif select_key = 'delete' or select_key = 'update'
Or select_key = 'insert '.
Update_flag = 'x '.
Endif.
Endloop. * display the SQL code
Sy-lsind = 0.
Delete itab_ SQL where line is initial.
Describe table itab_ SQL lines line_cnt.
If line_cnt = 0.
Write:/1 'edit your SQL ...... color 2.
Else.
Loop at itab_ SQL.
Write:/1 itab_sql-line.
Endloop.
Endif.
If update_flag = 'y '.
Exit.
Endif.
Endform. "editor_ SQL
*&------------------------------------------------------------------*
* & Form exec_modify
*&------------------------------------------------------------------*
Form exec_modify.
If sy-ucomm = 'exec '.
* Modify dialog box
Call function 'popup _ to_confirm_step'
Exporting
Textline1 = 'Do you want to really update? '
Titel = 'exit'
Importing
Answer = confirm_flag.
Case confirm_flag.
When 'n'. Exit. "No
When 'A'. Exit. "cancel
When 'J'. "perform exec_ SQL _update." Yes
Endcase.
Endif.
* Modify program ABAP code.
Itab_prog-line = 'program zsql19800526 message-ID .'.
Append itab_prog.
Itab_prog-line = 'data: Count type I .'.
Append itab_prog.
Itab_prog-line = 'form dyn2 .'.
Append itab_prog.
Itab_prog-line = 'exec SQL .'.
Append itab_prog.
Loop at itab_ SQL.
Itab_prog-line = itab_sql-line.
Append itab_prog.
Endloop.
Itab_prog-line = 'endexec .'.
Append itab_prog.
Itab_prog-line = 'message i315 with ''timed med ''SY-DBCNT '.
Concatenate itab_prog-line ''' records! ^-^ ''''''.'
Into itab_prog-line separated by space.
Append itab_prog.
Itab_prog-line = 'endform .'.
Append itab_prog. * dynamic program display
If sy-ucomm = 'edex '.
Call function 'editor _ application'
Exporting
Application = 'bf'
Display =''
Name = 'modify program ...'
Importing
Fcode = fcode
Tables
Content = itab_prog.
Stop.
Endif. * dynamic program excuted
Generate subroutine pool itab_prog name prog
Message MSG.
If sy-subrc <> 0.
Msg_text = MSG + (80 ).
Write:/1 msg_text.
Msg_text = MSG + 80 (40 ).
Write:/1 msg_text.
Else.
Perform dyn2 in program (Prog ).
Endif.
Endform. "exec_modify Program instance: 1. the program execution interface displays the SQL code input screen 2. the data we want to modify, material primary data material master table: material code primary key SOH-DL3C in Mara ---> SOH-DL8C3. write SQL statements for data modification. Here we will not detail the update Mara set matnr = 'soh-dl3c 'Where matnr = 'soh-dl8c'
And ersda = '000000' 4. Modify dynamic ABAP program generation confirmation modification 5. Dynamic Program Execution 6. program successful execution prompt information 7. Data Modification result confirmation

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.