[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 --> All SQL update statements in the ABAP program have the permission to execute, 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. ABAP dynamic program execution Technology
 
Note:
The SQL syntax must be accurate and the modification conditions must be accurate. The data uniqueness principle is not illegal after the data is modified.
 
Program code:
**************************************** ***************************
* (Copyright @ 2006 mysingle Digital System Co. Ltd.
* All Rights Reserved | confidential)
* System module: ABAP CBO
* Program description: dynamic SQL excute
* Developer: Zou Xin
* Develop Date: 2006.03.01
* Use status: Release 1.0
**************************************** ***************************
Report 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.
* Run the program and go to the ABAP code editor.
Set PF-STATUS 'pfsta00 '.
Write:/1 'edit your SQL ...... color 2.
 
At user-command.
* Dynamically generated program modification confirmation
If sy-ucomm = 'edit '.
Perform editor_ SQL.
* Dynamically generate Program Execution
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
 

I personally tested it. It is also possible to use the SELECT statement.

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.