"Transfer from http://blog.csdn.net/mysingle/article/details/678598"
Development note: In the SAP system maintenance process, sometimes we need to modify some table data, but a lot of table can not be directly modified in Tcode:se16, the use of SAP ID and no debug data modification permissions, then we should modify the data? The idea of the SQL UPDATE statement in the ABAP program has permission to execute as long as we can dynamically generate the ABAP code to modify the table field to execute dynamically! Development technology: 1. SQL code writing Technology 1. Dynamic program code generation technology 2.ABAP Dynamic Program Execution Technical Note: The SQL syntax must be accurate, modify the condition is accurate, after modifying the data is not illegal data uniqueness Principle Program code: The following ****************************** *************************************
* (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 (Ten) TYPE C,
etc (+) TYPE C,
Update_flag TYPE C,
line_cnt TYPE I,
Prog (8) TYPE C,
MSG (+) TYPE C,
Msg_text (*) 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 into the ABAP code Editor
SET pf-status ' PFSTA00 '.
WRITE:/1 ' Edit Your SQL ........ ' COLOR 2. At user-command.* Dynamic Generation Program Modification confirmation
IF Sy-ucomm = ' EDIT '.
PERFORM editor_sql.* Dynamic Generator 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 = ' Does want to really UPDATE? '
Titel = ' Exit '
Importing
Answer = Confirm_flag.
Case Confirm_flag.
When ' N '. EXIT. "NO
When ' A '. EXIT. The Cancel
When ' J '. "Perform exec_sql_update." YES
Endcase.
ENDIF.
* Modify program ABAP Code.
Itab_prog-line = ' program ZSQL19800526 Message-id at. '.
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 ' performed ' 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 Examples:1. Program execution interface, display SQL code entry screen 2. We want to modify the data, material master data material Master Table:mara in material code primary key SOH-DL3C---> soh-dl8c 3. Data modification SQL statement writing, which It's an unknown. UPDATE MARA SET matnr = ' soh-dl3c ' WHERE matnr = ' soh-dl8c '
and ERSDA = ' 20040310 ' 4. Modify dynamic ABAP program generation Confirmation modification 5. Dynamic program execution 6. The program successfully executes the prompt information 7. Data modification Effect Confirmation ok! Thank you~
ABAP Dynamic SQL Excute program for dynamically generating classic applications