首頁 Sap Abap
文章
Cancel

Sap Abap

常用T-Code的T-Code

這邊紀錄的是在導入SAP期間,我比較常接觸過的T-Code

雖然網路上google也能查到各模組的T-Code名稱 , 不過我這邊還是想用自己的方式記錄一下用途

T-Code用途描述
MB90重複列印MIGO過帳的文件
ME29N核發採購單
ME53N查採購單或請購單的資訊
ME54N核發請購單
MIGO收貨並列印收料單
PFCG維護系統帳號的權限角色
SE09核發Request
SE11建立結構、資料元素等等
SE16N查Table
SE37Function Module,常用在搜尋RFC
SE80找Source Code
SM37查詢或執行系統背景排程
STMS把核發的Request傳到PRD或QAS
SU01維護系統帳號
SU53User缺少權限被系統擋住時的Log會記錄在這
SPRO後台基本設定
OS01Ping測試
DBCO外部DB連線設定

常用T-Code的Table

因為開發客製的SAP報表時,時常需要從SE16N查詢資料表,而資料表常常又有數十個,甚至破百個欄位

所以我這邊記錄我自己常用的資料表及其欄位,以便未來可以比較快找到自己需要的資料

資料表名稱資料表欄位欄位名稱補充說明
RSEGBUZEI發票的項目 
RSEGEBELN採購文件的號碼 
RSEGEBELP採購文件的項目 
RSEGMATNR物料代號 
RSEGWERKS工廠 
RSEGMWSKZ稅碼 
RSEGLFBNR物料文件的號碼 
RSEGLFGJA物料文件的年度 
RSEGLFPOS物料文件的項次 
MAKTSPRAS語言 
MAKTMATNR物料代號 
MAKTMAKTX物料說明 
T024EKGRP採購群組代號 
T024EKNAM採購群組說明 
T156HTSPRAS語言 
T156HTBWART異動類型代號 
T156HTBTEXT異動類型說明 
ANLZBUKRS公司代碼 
ANLZANLN1主要資產號碼 
ANLZANLN2資產子號碼 
ANLZPERNR固定資產保管人 
ANLZADATU移轉生效時間 
agr_usersagr_name權限角色名稱 
agr_usersUNAME擁有這權限的人 
TSTCTTCODETCODE英文代號 
TSTCTTTEXTTCODE中文名稱 
AGR_TCODESAGR_NAME權限角色名稱 
AGR_TCODESTCODE屬於這權限角色的T-Code 
usr06BNAME帳號名稱 
usr06LIC_TYPE權限等級 
USR02BNAME帳號名稱 
USR02GLTGB帳號有效日期 
USR02TRDAT最後一次登入日期 
USR21BNAME帳號名稱 
USR21PERSNUMBER識別碼,串接adrp用的 
adrpPERSNUMBER識別碼,串接usr21用的 *主鍵 
adrpNAME_TEXT員工完整名稱 
PRPSPOSIDWBS元素:專案代號 

加速開發的工具

Abap Git

先導入AbapGit

https://docs.abapgit.org/

在用AbapGit導入SQL Editor

https://github.com/marianfoo/ztoad

有SQL Editor的話,每次要測試報表的Query的時候,就不必在額外寫ALV或進到中斷點看結果了, 但是要注意SQL Editor無法使用With as

Abap基本語法參考

宣告char的方式

1
2
DATA : MyChar(4) TYPE c.
DATA : MyString TYPE string.

迴圈的參數由來與使用方式大概像這樣

1
2
3
4
5
DATA : it_mara LIKE wa_head OCCURS 0 WITH HEADER LINE.
SELECT * from anlp
INTO CORRESPONDING FIELDS OF TABLE @it_mara.
loop at it_mara .
endloop.

迴圈中,更新workarea的方式,大概是長這樣 (it_mara是workarea )
更新的語法是MODIFY

1
2
3
4
5
6
7
8
DATA :Sum type i.
	      Sum = 0.
LOOP AT it_mara .
    Sum = Sum + 1.
    it_mara-MyCol_1 = Sum.
  it_mara-MyCol_2 = Sum * 2.
    MODIFY it_mara FROM it_mara  TRANSPORTING MyCol_1 MyCol_2
  ENDLOOP.

Method and Argument

1
2
3
4
5
6
7
8
9
10
11
12
13
 ******Call Method*********
 PERFORM MyMethod USING '參數一'   '參數二'.
 ******Creat Method*********
 FORM MyMethod USING
      font TYPE c
      Context  TYPE c.

  DATA : Temp1 TYPE c.
  DATA : Temp2 TYPE c.
  Temp1  = font.
  Temp2 = Context.
 
ENDFORM.

把SAP資料Insert到外部DB (MSSQL)的範例寫法

Desktop View

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
REPORT ConnetDB.

DATA wa_head TYPE ZMyHead. "ZMyHead是SE11中自定義的結構
DATA : it_mara LIKE wa_head OCCURS 0 WITH HEADER LINE.

PERFORM SQL_GetData_head. "把SAP資料放到WorkArea
PERFORM ConnectSQL. "開啟外部DB連線
PERFORM InsertToMSSQL.

*********取得Sap的資料***************************
FORM SQL_GetData_head.
    select * from anlz where anlz~ANLN1 = '100000000001'
    INTO CORRESPONDING FIELDS OF TABLE @it_mara.
ENDFORM.
*********與MIDDB建立連線************************
form ConnectSQL.
      exec sql.
      connect to 'MSSQL'
    endexec.
endform.
*********將WorkArea匯入到Mssql中****************
FORM InsertToMSSQL.
  LOOP AT it_mara.
    EXEC SQL.
      insert into MSSQL_DbName
      ( ColName_A             ,ColName_B

       )
      VALUES
      (:it_mara-ANLN1    ,:it_mara-BUKRS

       )
  ENDEXEC.
  endloop.
ENDFORM.

把外部DB (MSSQL)的資料,傳遞到SAP的WorkArea的範例寫法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
REPORT DEMO.
*********會用到的宣告*****
data: exc_ref    type ref to cx_sy_native_sql_error,
      error_text type string.
DATA wa_head TYPE zfi001_head. "自訂義的workarea
DATA : it_mara LIKE wa_head OCCURS 0 WITH HEADER LINE.
*******主程式-開始********

START-OF-SELECTION.
PERFORM  connectDB.
write : '結束-這邊可以下中斷點看it_mara'.

*******主程式-結束********

FORM connectDB.
try.
    exec sql.
      connect to 'MSSQL'
    endexec.
    perform loop_out.
catch cx_sy_native_sql_error into exc_ref.
    error_text = exc_ref->get_text( ).
    message error_text type 'I'.
endtry.
*  ===執行MSSQL索引====
  exec sql performing loop_out.
    SELECT
      Columns_Name
      INTO :wa_head-ANLN1 FROM Mssql_DbName
  endexec.
ENDFORM.

FORM loop_out.
  append wa_head to it_mara.
ENDFORM.

Abap Report Example

SAP Report

簡單的報表設計方式如下 Desktop View

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
REPORT Z_DEMO5.
*********ALV Call Function會用到的宣告
DATA :gt_events TYPE slis_t_event.
DATA :it_eban_b LIKE TABLE OF eban WITH HEADER LINE.
DATA:wa_layout TYPE slis_layout_alv.
     wa_layout-colwidth_optimize = 'X'.
DATA: lt_fields TYPE slis_t_fieldcat_alv.
*******定義結構******
DATA wa_head TYPE zfi001_head.
*******定義內表******
DATA : it_mara LIKE wa_head OCCURS 0 WITH HEADER LINE.
*******主程式********
START-OF-SELECTION.
  PERFORM SQL_GetData_head.
  PERFORM alv_output.
**********************
*******執行Query******
FORM SQL_GetData_head.
    select * from anlh
    INTO CORRESPONDING FIELDS OF TABLE @it_mara.
ENDFORM.

***************顯示報表介面**************************
FORM alv_output.
  CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
    EXPORTING
      i_program_name         = sy-repid
      i_internal_tabname     = 'IT_MARA'
      i_structure_name       = 'ZFI001_HEAD'
      i_inclname             = sy-repid
    CHANGING
      ct_fieldcat            = lt_fields
    EXCEPTIONS
      inconsistent_interface = 1
      program_error          = 2
      OTHERS                 = 3.
***************************************************
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program       = sy-repid
   "  i_callback_pf_status_set = 'SET_PF_STATUS'
   "  i_callback_user_command  = 'USER_COMMAND'
   "  i_callback_html_top_of_page = 'HTML_TOP-OF-PAGE' "see FORM
      i_save                   = 'A'
      is_layout                = wa_layout
      it_fieldcat              = lt_fields
      it_events                = gt_events[]
    TABLES
      t_outtab                 = it_mara.
ENDFORM.

備註:資料類型為CURR的項目,不能直接放入自定義的結構

必須自己新增資料元素跟範圍 在用Query的AS 處理

Desktop View

1
2
    select anlc~KNAFA as FI03  from anlc
    INTO CORRESPONDING FIELDS OF TABLE @it_mara.

Sql Example

with As使用方式

以下是With As子查詢的範例

因為我google很少看到單純with as 裡面有寫明確select的範例,所以這邊留個紀錄

備註:FI01跟FI02是it_mara裡面的結構欄位內容, it_mara是internal table

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 WITH
+anlh  as ( select * from anlh  ) ,
+anlc as (
select anlc~anln1 , anlc~gjahr, anlc~bukrs,
ANLC~NDABP,ANLC~NDABJ,
SUM( anlc~kansw + anlc~answl + anlc~kaufw ) as FI01,
SUM( anlc~kansw + anlc~answl + anlc~kaufw + anlc~knafa + anlc~nafag + anlc~AAFAG ) as  FI02
from anlc
group by anlc~anln1 , anlc~gjahr ,anlc~bukrs,ANLC~NDABP,ANLC~NDABJ
)
SELECT
+anlh~bukrs,
+anlc~FI01, "FI01是 it_mara這個自定義結構中的自定義欄位名稱,字典類型是用自定義的DEC
+anlc~FI02, "FI02是 it_mara這個自定義結構中的自定義欄位名稱,字典類型是用自定義的DEC
FROM +anlh
LEFT JOIN +anlc ON +anlh~anln1 = +anlc~anln1
INTO CORRESPONDING FIELDS OF TABLE @it_mara.

稽核用Query

備註:要搭配「Sql Edit」,不然需要另外製作Structure會很麻煩 而且以下Query基本上只要是有用SAP的公司,都適用才對,畢竟只是Query,

進貨資料

如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
select  
LFA1~LIFNR
,LFA1~SORTL
,LFA1~MCOD1
,LFA1~STCD1
,LFA1~KTOKK
,LFM1~ZTERM "付款條件碼
,T052U~TEXT1 "付款條件名稱
,LFBK~BANKN"銀行帳戶
,BNKA~BANKL "銀行碼
,BNKA~BANKA "銀行名稱
,TCURT~WAERS
,TCURT~KTEXT
from LFA1
left join LFM1 on LFM1~LIFNR =  LFA1~LIFNR
left join LFBK on LFBK~LIFNR =  LFA1~LIFNR
left join T052U on T052U~ZTERM = LFM1~ZTERM  
left join BNKA on BNKA~BANKL =  LFBK~BANKL
left join TCURT on TCURT~WAERS =  LFM1~WAERS and TCURT~SPRAS = @SY-LANGU

採購資料

如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
select  distinct
 EKKO~BSART"採購文件類型
,T161T~BATXT
,EKKO~EBELN"採購單號
,EKKO~BEDAT"文件日期
,EKKO~LIFNR"供應商
,EKKO~PROCSTAT"採購文件處理狀態
,LFA1~MCOD1
,EKKO~ZTERM"付款條件
,T052U~TEXT1
",EKKO~SPRAS"語言代碼
,EKKO~EKGRP"採購群組
,T024~EKNAM
,EKKO~WAERS"幣別
,EKKO~WKURS"匯率
,EKKO~VERKF"業務人員
,EKKO~RLWRT"核發價值(採購金額)
,EKPO~EBELN
,EKPO~WERKS
,T001W~NAME1
,EKPO~MWSKZ
,T007S~TEXT1
  from EKKO
  left join T161T on T161T~BSART  = EKKO~BSART and  T161T~SPRAS = @SY-LANGU
  left join T024 on T024~EKGRP = EKKO~EKGRP
  left join LFA1 on LFA1~LIFNR = EKKO~LIFNR
  left join T052U on T052U~ZTERM = EKKO~ZTERM and  T052U~SPRAS = @SY-LANGU
  left join EKPO on  EKPO~EBELN = EKKO~EBELN
  left join T001W on T001W~WERKS = EKPO~WERKS
  left join T007S on T007S~MWSKZ =  EKPO~MWSKZ and T007S~SPRAS = @SY-LANGU
  order by EKKO~EBELN

供應商資料

如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
select distinct
RBKP~LIFNR "開出這發票的供應商的代號
,LFA1~MCOD1  "開出這發票的供應商的名稱
,RBKP~XBLNR "發票號碼
,T007S~TEXT1 "發票格式說明
,MSEG~BUDAT_MKPF "過至日期
,MSEG~MBLNR "物料文件號碼
,MSEG~MJAHR "物料文件年度
,MSEG~EBELN "採購單單號
,MSEG~EBELP "採購單項次
,EKPO~BANFN
,EKPO~BNFPO
,MSEG~BWART "異動類型代號
,T156HT~BTEXT "異動類型名稱
,MSEG~MATNR  "物料代號
,MAKT~MAKTX "物料名稱
,EKPO~TXZ01 "收貨項目說明
,EKPO~NETPR "單價
",EKBE~MENGE "收貨數量
,EKBE~BPMNG "單位數量
,EKBE~WRBTR "金額
,EKBE~WAERS "幣別
,MSEG~WERKS "收貨工廠代號
,T001W~Name1 "收貨工廠名稱
,MSEG~LGORT "儲存倉庫代號
,T001L~LGOBE "儲存倉庫名稱
,EKKO~EKGRP "採購群組代號
,T024~EKNAM "採購群組名稱
from MSEG
left join  MAKT on MAKT~MATNR = mseg~MATNR
left join T001W on T001W~WERKS = mseg~WERKS
left join T001L on T001L~LGORT = mseg~LGORT
left join RSEG on RSEG~LFBNR = mseg~LFBNR
              and RSEG~LFGJA = mseg~MJAHR
              and RSEG~LFPOS = mseg~ZEILE
left join RBKP on RBKP~BELNR = RSEG~BELNR
              and RBKP~GJAHR = RSEG~GJAHR
left join LFA1 on LFA1~LIFNR = RBKP~LIFNR
left join EKKO on EKKO~EBELN = MSEG~EBELN
left join T024 on T024~EKGRP = EKKO~EKGRP
left join T156HT on T156HT~BWART = MSEG~BWART
               and T156HT~SPRAS = @SY-LANGU
left join EKBE on EKBE~EBELN = MSEG~EBELN
              and EKBE~EBELP = MSEG~EBELP
left join EKPO on EKPO~EBELN = MSEG~EBELN
              and EKPO~EBELP = MSEG~EBELP
left join T007S on T007S~MWSKZ = RBKP~MWSKZ1 and T007S~SPRAS = @SY-LANGU
order by  MSEG~MJAHR,MSEG~MBLNR
本文由作者按照 CC BY 4.0 進行授權