1. 用NATAIVE SQL做分页取数
2. 选择屏幕动态选择子屏幕
3. 程序生成IDOC例子
4. 限制同名程序同时只运行一个
5. 翻转字符串
6. 广泛使用的一个针对特定在线用户发送系统消息的程序
7. SAP发送工作台快件
8. SAVE LIST to client
1. 用NATAIVE SQL做分页取数
REPORT Z_BARRY_TEST NO STANDARD PAGE HEADING .
DATA: BEGIN OF ITAB OCCURS 0 ,
EBELN LIKE EKKO-EBELN ,
BUKRS LIKE EKKO-BUKRS ,
END OF ITAB.
DATA: m type i ,
n TYPE i ,
f TYPE i .
m = 10 . "起始记录
n = 10 . "要取的记录条数
f = m + n .
*EXEC SQL PERFORMING add.
* SELECT bukrs INTO :ekko-bukrs FROM EKKO where rownum < 6
*ENDEXEC.
EXEC SQL PERFORMING append.
SELECT t2.ebeln,t2.bukrs INTO :ITAB FROM
(select rownum rown,t1.* from ekko t1
where rownum <= :f and mandt = :sy-mandt
and LENGTH(ERNAM) > 6
) t2 where rown > :m
ENDEXEC.
LOOP AT ITAB.
WRITE: / ITAB-EBELN ,ITAB-BUKRS .
ENDLOOP.
FORM APPEND.
APPEND ITAB.
ENDFORM.
2. 选择屏幕动态选择子屏幕
REPORT zrs08212 .
TABLES: usr02,
sscrfields.
*---------------------------------------------------------------
* SUBSCREEN 1
*---------------------------------------------------------------
SELECTION-SCREEN BEGIN OF SCREEN 100 AS SUBSCREEN.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-010.
SELECT-OPTIONS: username FOR usr02-bname .
SELECTION-SCREEN END OF BLOCK b1.
SELECTION-SCREEN END OF SCREEN 100.
*---------------------------------------------------------------
* SUBSCREEN 2
*---------------------------------------------------------------
SELECTION-SCREEN BEGIN OF SCREEN 200 AS SUBSCREEN.
SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-020.
SELECT-OPTIONS: lastlogi FOR usr02-trdat.
SELECTION-SCREEN END OF BLOCK b2.
SELECTION-SCREEN END OF SCREEN 200.
*---------------------------------------------------------------
* SUBSCREEN 3
*---------------------------------------------------------------
SELECTION-SCREEN BEGIN OF SCREEN 300 AS SUBSCREEN.
SELECTION-SCREEN BEGIN OF BLOCK b3 WITH FRAME TITLE text-030.
SELECT-OPTIONS: classtyp FOR usr02-class.
SELECTION-SCREEN END OF BLOCK b3.
SELECTION-SCREEN END OF SCREEN 300.
SELECTION-SCREEN: FUNCTION KEY 1,
FUNCTION KEY 2.
SELECTION-SCREEN: BEGIN OF TABBED BLOCK sub FOR 15 LINES,
END OF BLOCK sub.
START-OF-SELECTION.
SELECT * FROM usr02 WHERE bname IN username
AND erdat IN lastlogi
AND class IN classtyp.
WRITE: / 'User ', usr02-bname,
'Last Login Date ', usr02-trdat,
'Last Login Time ', usr02-ltime,
'CLASS ', usr02-class.
ENDSELECT.
INITIALIZATION.
sscrfields-functxt_01 = '@0D@'.
sscrfields-functxt_02 = '@0E@'.
sub-prog = sy-repid.
sub-dynnr = 100.
AT SELECTION-SCREEN.
CASE sy-dynnr.
WHEN 100.
IF sscrfields-ucomm = 'FC01'.
sub-dynnr = 300.
ELSEIF sscrfields-ucomm = 'FC02'.
sub-dynnr = 200.
ENDIF.
WHEN 200.
IF sscrfields-ucomm = 'FC01'.
sub-dynnr = 100.
ELSEIF sscrfields-ucomm = 'FC02'.
sub-dynnr = 300.
ENDIF.
WHEN 300.
IF sscrfields-ucomm = 'FC01'.
sub-dynnr = 200.
ELSEIF sscrfields-ucomm = 'FC02'.
sub-dynnr = 100.
ENDIF.
ENDCASE.
3. 程序生成IDOC例子
REPORT zrs08235 .
DATA: g_idoc_ctl LIKE edidc,
g_e1maram LIKE e1maram,
g_e1maktm LIKE e1maktm.
DATA: gi_ret_ctl LIKE edidc OCCURS 0,
gi_idoc_data LIKE edidd OCCURS 0 WITH HEADER LINE.
* BUILD IDOC_CTL RECORD
PERFORM build_idoc_ctl.
* BUILD ICOC_SEGMENT DATA
PERFORM build_idoc_seg.
* CALL FUNCTION AND CREATE IDOC
PERFORM create_idoc.
*&---------------------------------------------------*
*& form BUILD_IDOC_CTL
*&--------------------------------------------------*
FORM build_idoc_ctl.
* TYPE
g_idoc_ctl-idoctp = 'ZMMIDOC'. "IDOC TYPE
g_idoc_ctl-mestyp = 'ZMMCRT'. "MESSAGE TYPE
* RECEIVER
g_idoc_ctl-rcvprt = 'LS'. "PARTNER TYPE
g_idoc_ctl-rcvprn = 'MMRECV800'. "PARTNER NO
* SENDER
g_idoc_ctl-sndprt = 'LS'. "PARTNER TYPE
g_idoc_ctl-sndprn = 'T90CLNT090'. "PARTNER NO
g_idoc_ctl-sndpor = 'SAPC11'. "PORT
ENDFORM. " BUILD_IDOC_CTL
*&---------------------------------------------------*
*& form BUILD_IDOC_SEG
*&---------------------------------------------------*
FORM build_idoc_seg.
* HEAD DATA
gi_idoc_data-segnam = 'E1MARAM'. "SEGMENT NAME
gi_idoc_data-segnum = 1. "SEGMENT NO
g_e1maram-matnr = 'IDOCTEST15'. "MM NO
g_e1maram-meins = '10'. "BASE UOM
g_e1maram-mbrsh = 'M'. "INDUSTRY
g_e1maram-mtart = 'DIEN'. "MM TYPE
MOVE g_e1maram TO gi_idoc_data-sdata.
APPEND gi_idoc_data.
* DESCRIPTION
gi_idoc_data-segnam = 'E1MAKTM'. "SEGMENT NAME
gi_idoc_data-segnum = 2. "SEGMENT NO
g_e1maktm-spras = sy-langu. "LANGUAGE
g_e1maktm-maktx = 'IDOC TEST'. "DESCRIPTION
MOVE g_e1maktm TO gi_idoc_data-sdata.
APPEND gi_idoc_data.
ENDFORM. " BUILD_IDOC_SEG
*&---------------------------------------------------*
*& form CREATE_IDOC
*&---------------------------------------------------*
FORM create_idoc.
CALL FUNCTION 'MASTER_IDOC_DISTRIBUTE'
EXPORTING
master_idoc_control = g_idoc_ctl
TABLES
communication_idoc_control = gi_ret_ctl
master_idoc_data = gi_idoc_data
EXCEPTIONS
error_in_idoc_control = 1
error_writing_idoc_status = 2
error_in_idoc_data = 3
sending_logical_system_unknown = 4
OTHERS = 5.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ELSE.
COMMIT WORK.
ENDIF.
ENDFORM. " CREATE_IDOC
4. 限制同名程序同时只运行一个
REPORT z_barry_thwpinfo .
DATA : th_opcode(1) TYPE x,
with_cpu TYPE x VALUE 0.
CONSTANTS: opcode_wp_list LIKE th_opcode VALUE 1.
DATA: BEGIN OF wp_tabl OCCURS 10.
INCLUDE STRUCTURE wpinfo.
DATA: END OF wp_tabl.
FREE : wp_tabl.
CALL 'ThWpInfo'
ID 'OPCODE' FIELD opcode_wp_list
ID 'TAB' FIELD wp_tabl-*sys*
ID 'CPU' FIELD with_cpu.
LOOP AT wp_tabl.
IF wp_tabl-wp_report = 'Z_BARRY_THWPINFO' .
MESSAGE E000(oo) WITH '已经有同名程序在运行!'.
ENDIF .
ENDLOOP.
5. 翻转字符串
REPORT z_barry_reverse NO STANDARD PAGE HEADING .
DATA: c1(20) TYPE c ,
c2(20) TYPE c.
c1 = '赵兄托你帮我办点事'.
CALL FUNCTION 'STRING_REVERSE'
EXPORTING
string = c1
lang = '1'
IMPORTING
rstring = c2
EXCEPTIONS
too_small = 1
OTHERS = 2.
WRITE c2.
6. 广泛使用的一个针对特定在线用户发送系统消息的程序
REPORT z_send_message NO STANDARD PAGE HEADING LINE-SIZE 120 .
INCLUDE <list>.
DATA: BEGIN OF usr_tabl OCCURS 0.
INCLUDE STRUCTURE uinfo.
DATA: END OF usr_tabl.
DATA: l_length TYPE i,
t_abaplist LIKE abaplist OCCURS 0 WITH HEADER LINE.
DATA: BEGIN OF t_user OCCURS 0,
counter TYPE i,
selection TYPE c,
mandt LIKE sy-mandt,
bname LIKE sy-uname,
name_first LIKE v_adrp_cp-name_first,
name_last LIKE v_adrp_cp-name_last,
department LIKE v_adrp_cp-department,
tel_number LIKE v_adrp_cp-tel_number,
END OF t_user .
DATA: l_client LIKE sy-mandt,
l_userid LIKE uinfo-bname,
l_opcode TYPE x,
l_funct_code(1) TYPE c,
l_test(200) TYPE c.
l_opcode = 2.
CALL 'ThUsrInfo' ID 'OPCODE' FIELD l_opcode
ID 'TAB' FIELD usr_tabl-*sys* .
CLEAR t_user.
REFRESH t_user.
LOOP AT usr_tabl.
t_user-mandt = usr_tabl-mandt.
t_user-bname = usr_tabl-bname.
APPEND t_user.
ENDLOOP.
SORT t_user.
DELETE ADJACENT DUPLICATES FROM t_user.
LOOP AT t_user.
t_user-counter = sy-tabix.
SELECT v~name_first
v~name_last
v~department
v~tel_number
INTO (t_user-name_first,
t_user-name_last,
t_user-department,
t_user-tel_number)
FROM usr21 AS u
JOIN v_adrp_cp AS v
ON u~persnumber = v~persnumber AND
u~addrnumber = v~addrnumber
WHERE u~bname = t_user-bname.
ENDSELECT.
MODIFY t_user.
ENDLOOP.
SORT t_user BY department name_last.
PERFORM display_list.
TOP-OF-PAGE.
PERFORM display_menu.
* End of top-of-page
TOP-OF-PAGE DURING LINE-SELECTION .
PERFORM display_menu.
* End of top-of-page during line-selection
AT LINE-SELECTION.
IF sy-curow = 2.
IF sy-cucol < 19.
t_user-selection = 'X'.
MODIFY t_user TRANSPORTING selection WHERE selection = ''.
PERFORM display_list.
ELSEIF sy-cucol < 36.
CLEAR t_user-selection.
MODIFY t_user TRANSPORTING selection WHERE selection = 'X'.
PERFORM display_list.
ELSEIF sy-cucol < 50.
PERFORM transfer_selection.
PERFORM popup_msg.
ELSEIF sy-cucol < 67.
PERFORM transfer_selection.
SORT t_user BY name_last.
PERFORM display_list.
ELSEIF sy-cucol < 81.
PERFORM transfer_selection.
SORT t_user BY name_first.
PERFORM display_list.
ELSEIF sy-cucol < 93.
PERFORM transfer_selection.
SORT t_user BY mandt.
PERFORM display_list.
ENDIF.
ENDIF.
*&---------------------------------------------------------------------*
*& Form DISPLAY_LIST
*&---------------------------------------------------------------------*
FORM display_list.
sy-lsind = 0.
FORMAT COLOR COL_NORMAL INTENSIFIED OFF.
LOOP AT t_user.
WRITE: / sy-vline, t_user-selection AS CHECKBOX,
sy-vline, t_user-mandt,
sy-vline, t_user-bname,
sy-vline, t_user-name_first(15),
sy-vline, t_user-name_last(15),
sy-vline, t_user-department(20),
sy-vline, t_user-tel_number(20), sy-vline.
HIDE: t_user-counter, t_user-selection.
ENDLOOP.
FORMAT COLOR OFF.
WRITE: /(108) sy-uline.
ENDFORM. " DISPLAY_LIST
*&---------------------------------------------------------------------*
*& Form DISPLAY_MENU
*&---------------------------------------------------------------------*
FORM display_menu.
FORMAT COLOR COL_HEADING HOTSPOT.
WRITE: (91) sy-uline,
/ sy-vline NO-GAP, (4) icon_select_all NO-GAP, '选择全部 ',
sy-vline NO-GAP, (4) icon_deselect_all NO-GAP, '全部不选择 ',
sy-vline NO-GAP, (4) icon_short_message NO-GAP, '发送消息 ',
sy-vline NO-GAP, (4) icon_sort_up NO-GAP, 'Last Name' NO-GAP,
sy-vline NO-GAP, (4) icon_sort_up NO-GAP, 'First Name' NO-GAP,
sy-vline NO-GAP, (4) icon_sort_up NO-GAP, 'Client' NO-GAP,
sy-vline,
/(91) sy-uline,
/(108) sy-uline.
FORMAT HOTSPOT OFF.
WRITE: / sy-vline, ' ',
sy-vline, 'Cli',
sy-vline, 'User ',
sy-vline, 'First Name ',
sy-vline, 'Last Name ',
sy-vline, 'Department ',
sy-vline, 'Telephone ',
sy-vline,
/(108) sy-uline.
FORMAT COLOR OFF.
ENDFORM. " DISPLAY_MENU
*&---------------------------------------------------------------------*
*& Form TRANSFER_SELECTION
*&---------------------------------------------------------------------*
FORM transfer_selection.
DO.
READ LINE sy-index FIELD VALUE t_user-selection.
IF sy-subrc <> 0.
EXIT.
ENDIF.
MODIFY t_user TRANSPORTING selection
WHERE counter = t_user-counter.
ENDDO.
CLEAR t_user.
ENDFORM. " TRANSFER_SELECTION
*&---------------------------------------------------------------------*
*& Form POPUP_MSG
*&---------------------------------------------------------------------*
FORM popup_msg.
DATA: l_msg LIKE sm04dic-popupmsg VALUE 'Experimental Message',
l_len TYPE i,
l_ret TYPE c.
LOOP AT t_user WHERE selection = 'X'.
PERFORM get_message CHANGING l_msg l_ret.
EXIT.
ENDLOOP.
IF l_ret = 'A'. "User cancelled the message
EXIT.
ENDIF.
* Get the message text
l_len = STRLEN( l_msg ).
LOOP AT t_user WHERE selection = 'X'.
CALL FUNCTION 'TH_POPUP'
EXPORTING
client = t_user-mandt
user = t_user-bname
MESSAGE = l_msg
message_len = l_length
* CUT_BLANKS = ' '
EXCEPTIONS
user_not_found = 1
OTHERS = 2.
IF sy-subrc <> 0.
WRITE: 'User ', t_user-bname, 'not found.'.
ENDIF.
ENDLOOP.
IF sy-subrc <> 0.
* Big error! No user has been selected.
MESSAGE ID 'AT' TYPE 'E' NUMBER '315' WITH '至少选择一个用户!'.
EXIT.
ENDIF.
ENDFORM. " POPUP_MSG
*&---------------------------------------------------------------------*
*& Form GET_MESSAGE
*&---------------------------------------------------------------------*
FORM get_message CHANGING p_l_msg LIKE sm04dic-popupmsg
p_returncode TYPE c.
DATA: BEGIN OF fields OCCURS 1.
INCLUDE STRUCTURE sval.
DATA: END OF fields,
returncode TYPE c.
fields-tabname = 'SM04DIC'.
fields-fieldname = 'POPUPMSG'.
fields-fieldtext = 'Message :'.
CONCATENATE ' - Msg from' sy-uname '.' INTO fields-value SEPARATED BY ' '.
APPEND fields.
CALL FUNCTION 'POPUP_GET_VALUES'
EXPORTING
popup_title = 'Supply the popup message'
IMPORTING
returncode = p_returncode
TABLES
fields = fields.
IF p_returncode = 'A'.
EXIT.
ELSE.
READ TABLE fields INDEX 1.
p_l_msg = fields-value.
ENDIF.
ENDFORM. " GET_MESSAGE
7. SAP发送工作台快件
REPORT z_barry_send_express_mail.
PARAMETERS: p_uname(40) TYPE c DEFAULT 'BAITZ' .
DATA: it_packing_list LIKE sopcklsti1 OCCURS 0 WITH HEADER LINE,
it_contents LIKE solisti1 OCCURS 0 WITH HEADER LINE,
it_receivers LIKE somlreci1 OCCURS 0 WITH HEADER LINE,
gd_sent_all(1) TYPE c,
gd_doc_data LIKE sodocchgi1 .
START-OF-SELECTION.
PERFORM send_email .
*&---------------------------------------------------------------------*
*& Form SEND_EMAIL_MESSAGE
*&---------------------------------------------------------------------*
FORM send_email .
APPEND 'asfasfadsf' TO it_contents.
APPEND 'asfasdfasfafasfa' TO it_contents.
gd_doc_data-obj_langu = sy-langu.
gd_doc_data-obj_descr = '此处填主题'.
CLEAR it_packing_list.
REFRESH it_packing_list.
it_packing_list-transf_bin = space.
it_packing_list-head_start = 1.
it_packing_list-head_num = 0.
it_packing_list-body_start = 1.
DESCRIBE TABLE it_contents LINES it_packing_list-body_num.
it_packing_list-doc_type = 'RAW'.
APPEND it_packing_list.
CLEAR it_receivers.
REFRESH it_receivers.
it_receivers-receiver = p_uname.
it_receivers-rec_type = 'B'.
it_receivers-com_type = 'INT'.
it_receivers-notif_del = 'X'.
it_receivers-notif_ndel = 'X'.
it_receivers-express = 'X'.
APPEND it_receivers.
CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
EXPORTING
document_data = gd_doc_data
put_in_outbox = 'X'
commit_work = 'X'
IMPORTING
sent_to_all = gd_sent_all
TABLES
packing_list = it_packing_list
contents_txt = it_contents
receivers = it_receivers
EXCEPTIONS
too_many_receivers = 1
document_not_sent = 2
document_type_not_exist = 3
operation_no_authorization = 4
parameter_error = 5
x_error = 6
enqueue_error = 7
OTHERS = 8.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM. "send_email
或者使用简单方法,只能发送文本:
REPORT zrs08199 .
DATA: ztext LIKE soli OCCURS 0 WITH HEADER LINE .
PARAMETERS: uname LIKE usr02-bname .
START-OF-SELECTION .
EDITOR-CALL FOR ztext.
CALL FUNCTION 'RS_SEND_MAIL_FOR_SPOOLLIST'
EXPORTING
mailname = 'aaa'
mailtitel = 'bbb'
user = uname
TABLES
text = ztext
EXCEPTIONS
error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
8. SAVE LIST to client
REPORT z_barry_test .
WRITE / 'aa'.
WRITE / '测试中文'.
PERFORM save_list USING '/usr/sap/listtext.txt' 'LIST20090617'.
*PERFORM write_list_from_db USING 'LIST20090617'.
*&---------------------------------------------------------------------*
*& Form save_list
*&---------------------------------------------------------------------*
FORM save_list USING dname id.
DATA: it_list LIKE abaplist OCCURS 0 WITH HEADER LINE ,
it_ascii LIKE w3html OCCURS 0 WITH HEADER LINE .
DATA wa_indx TYPE indx.
CALL FUNCTION 'SAVE_LIST'
EXPORTING
list_index = '0'
force_write = 'X'
TABLES
listobject = it_list
EXCEPTIONS
list_index_invalid = 1
OTHERS = 2.
**如果要放入簇数据库
wa_indx-aedat = sy-datum.
wa_indx-usera = sy-uname.
wa_indx-pgmid = sy-repid.
EXPORT itab = it_list TO DATABASE indx(lt) ID id FROM wa_indx .
CALL FUNCTION 'LIST_TO_ASCI'
TABLES
listasci = it_ascii
listobject = it_list
EXCEPTIONS
empty_list = 1
list_index_invalid = 2
OTHERS = 3.
**下载到本地
* CALL FUNCTION 'GUI_DOWNLOAD'
* EXPORTING
* filename = 'C:\list.txt'
* TABLES
* data_tab = it_ascii.
**存储为Dataset
OPEN DATASET dname FOR OUTPUT IN TEXT MODE ENCODING UTF-8.
LOOP AT it_ascii .
TRANSFER it_ascii TO dname.
TRANSFER cl_abap_char_utilities=>cr_lf TO dname.
ENDLOOP.
CLOSE DATASET dname.
**下载Dataset到本地
* CALL FUNCTION 'C13Z_FILE_DOWNLOAD_BINARY'
* EXPORTING
* i_file_front_end = 'c:\list.txt'
* i_file_appl = dname
* i_file_overwrite = 'X'
* EXCEPTIONS
* fe_file_open_error = 1
* fe_file_exists = 2
* fe_file_write_error = 3
* ap_no_authority = 4
* ap_file_open_error = 5
* ap_file_empty = 6
* OTHERS = 7.
ENDFORM. "save_list
*&---------------------------------------------------------------------*
*& Form write_list_from_db
*&---------------------------------------------------------------------*
FORM write_list_from_db USING id .
DATA: it_list LIKE abaplist OCCURS 0 WITH HEADER LINE ,
it_ascii LIKE w3html OCCURS 0 WITH HEADER LINE .
DATA wa_indx TYPE indx.
IMPORT itab = it_list FROM DATABASE indx(lt) ID id.
CALL FUNCTION 'WRITE_LIST'
EXPORTING
write_only = 'X'
TABLES
listobject = it_list
EXCEPTIONS
empty_list = 1
OTHERS = 2.
ENDFORM. "read_list_from_db