[SAP]ABAP动态生成经典应用之Dynamic SQL Excute 程序

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/mysingle/article/details/678598
开发说明:在SAP的系统维护过程中,有时我们需要修改一些Table中的数据,可是很多Table又不能直接在Tcode:SE16中修改,使用的SAP ID又没有调试数据修改权限,这时我们应该怎么样修改数据呢?思路--> ABAP程序中的SQL 更新语句谁都有权限执行,只要我们能动态生成修改该Table字段的ABAP CODE动态执行即可!
 
开发技术:
1.SQL代码编写技术
1.动态程序代码生成技术
2.ABAP动态程序执行技术
 
注意事项:
SQL语法一定要准确,修改条件准确,修改数据后不违法数据唯一性原则
 
程序代码:如下
*******************************************************************
* (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.
*程序执行直接进入ABAP代码编辑器
  SET PF-STATUS 'PFSTA00'.
  WRITE: /1 'Edit Your SQL ................' COLOR 2.
 
AT USER-COMMAND.
*动态生成程序修改确认
  IF sy-ucomm = 'EDIT'.
    PERFORM editor_sql.
*动态生成程序执行
  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 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
 
程序实例:
 
1.程序执行界面,显示SQL CODE录入画面
 
 
 
2.我们要修改的数据,材料主数据Material Master Table:MARA中的材料CODE主键
SOH-DL3C ---> SOH-DL8C
3.数据修改SQL语句编写,这里就不详叙了
 
UPDATE MARA SET MATNR = 'SOH-DL3C' WHERE MATNR = 'SOH-DL8C'
                                    AND ERSDA = '20040310'
 
 
 
 
4.修改动态ABAP程序生成确认修改
 
 
5.动态程序执行
 
 
 
6.程序成功执行提示信息
 
 
 
7.数据修改效果确认
 
OK!Thank you~
展开阅读全文

没有更多推荐了,返回首页