ABAP Basic Concept (2) 運算子 控制式與迴圈 SQL指令 Subroutines Event Block IF, CASE, DO… ENDDO SQL指令 Subroutines Call by value, value and result, reference Event Block Initialization, Start-of-Selection, At Line-Selection
1. 運算子 比較運算子 = , <> , < , <= , > , >= 比較運算子 = , <> , < , <= , > , >= 算數運算子 + , - , * , /, mod (餘數) 邏輯運算子 AND , OR
2. 控制式與迴圈 IF子句 IF <Logical Expression> . <Statements>. ELSEIF <Logical Expression> . <Statements>. ELSE . <Statements>. ENDIF . CASE子句 CASE < variable > . WHEN 'value1' . <Statements>. WHEN 'value2' OR 'value3' . <Statements>. WHEN OTHERS . <Statements>. ENDCASE .
控制式與迴圈 (Cont.) DO…ENDDO子句 DO 整數變數 TIMES. ENDDO. 範例: DATA X TYPE I. DATA N TYPE I. X = 1. N = 5. DO N TIMES. WRITE: / X. X = X + 1.
3. SQL 指令 SELECT < result > 哪些欄位 FROM < table > 從哪個table INTO < destination > 複製到哪 WHERE < condition >. 條件 SELECT SINGLE * 從database中只挑選一列符合條件的資料 INTO指令與CORRESPONDING FIELDS OF…連用
SQL 指令 (Cont.) SELECT…… ENDSELECT 為一種迴圈結構,其中的程式段會被反覆執行直到資料庫讀取結束 範例:印出zplayer table中所有球員的姓名 SELECT pno pname FROM zplayer INTO CORRESPONDING FIELDS OF wa_player. WRITE: /20 wa_player-pname. ENDSELECT.
練習D 印出TABLE內容 (程式名稱zyy**_ex_d) 使用SELECT …ENDSELECT,印出zyy**_EMP該Table中,CLASS欄位為A1的所有記錄。 判斷是否有記錄,若無則印出There is no record in the table;若有資料則印出Table has at least one record。
練習D參考解答 REPORT zyyTC_EX_D . TABLES zyyTC_EMP. SELECT * FROM zyyTC_EMP WHERE CLASS = 'A1'. WRITE : / zyyTC_EMP-EMPID, zyyTC_EMP-DEPID, zyyTC_EMP-CLASS, zyyTC_EMP-SEX, zyyTC_EMP-BIRTH, zyyTC_EMP-ADDRESS. ENDSELECT. IF SY-SUBRC <> 0. WRITE : / 'There is no record in the table.'. ELSE. WRITE : / 'Table has at least one record.'. ENDIF.
練習E 將abap-dictionary之TABLE複製到Internal Table (程式名稱zyy**_ex_e) 宣告表格zyy**_EMP。 宣告一Work Area變數,結構如zyy**_EMP 宣告一Internal Table變數,型態如zyy**_EMP 利用SQL指令,將zyy**_EMP逐筆複製到Work Area,再將之新增入Internal Table 印出Internal Table所有內容。
練習E參考解答 REPORT zyyTC_EX_E . TABLES zyyTC_EMP. DATA wa LIKE zyyTC_EMP. DATA itab TYPE STANDARD TABLE OF zyyTC_EMP WITH HEADER LINE. SELECT * FROM zyyTC_EMP INTO CORRESPONDING FIELDS OF wa. APPEND wa TO itab. ENDSELECT. LOOP AT itab. WRITE: / itab. ENDLOOP.
4. Subroutines 第一行以FORM開始,最後一行以ENDFORM結束
參數傳遞方式 相同記憶體位址 CHANGING fv3 USING VALUE (fv1) 不同位址,但回傳後更改其值 呼叫副程式 USING VALUE (fv1) 不同位址,但回傳後更改其值 CHANGING VALUE (fv2) 相同記憶體位址 CHANGING fv3 CHANGING
練習F 計算總和副程式 (程式名稱zyy**_ex_f) 定義整數變數n,並設定n為6,請分別呼叫以下兩支副程式,再印出n值。 副程式1:利用call by value 根據傳來的n值,印出n平方 副程式2:利用call by reference
練習F參考解答 REPORT zyyTC_EX_F . DATA N TYPE I. N = 6. PERFORM SQUARE1 USING N. WRITE: / 'N=', N. ULINE. PERFORM SQUARE2 CHANGING N. FORM SQUARE1 USING VALUE(X). X = X * X. WRITE: / 'Square1: N^2=', X. ENDFORM. FORM SQUARE2 CHANGING X.
5. Event Block 類似視窗系統之事件,無一定之執行順序,而由 runtime system 所決定 用法: 第一行以keyword為起始,無結束keyword 直到遇到下一個event block或FORM keyword才結束
Event Blocks for Reports
Event Blocks for Reports (Cont.) The runtime environment creates the INITIALIZATION event and calls the event block. If there is a selection screen defined in the program, control returns to the selection screen processor. Control then passes to the reporting processor. It creates the START-OF-SELECTION event. The logical database, if you are using one, calls further event blocks at this point. The reporting processor creates the END-OF-SELECTION event. If the program contains a list description, it displays the list defined in the AT LINE-SELECTION event.
Major Event Blocks 設定初值 Basic list要用到的 event,用來產生report 範例 PARAMETERS INPUTN TYPE I. START-OF-SELECTION. IF INPUTN = 1 . WRITE : 'HI!'. ENDIF.
Major Event Blocks (Cont.) AT LINE-SELECTION Detail list所用到的 event 報表Drill-down(向下挖掘)之用 可利用 IF 來控制選擇層數:Basic list有參數值sy-lsind = 0,第一層sy-lsind = 1,最多可至20層 範例 REPORT …… ……………… AT LINE-SELECTION. IF sy-lsind = 1. SELECT * FROM spfli INTO CORRESPONDING FIELDS OF wa_flight WRITE :/ wa_flight-carrid, wa_flight-connid. ENDSELECT. IF sy-lsind = 2.