# ABAP程式設計 WORKAREA 新增修改刪除 ###### tags: `ABAP程式設計` ##### Updating Databases With Open SQL ![](https://i.imgur.com/kCwxDR4.png) ### INSERT 資料庫的新增 - Creating a Single Record ``` INSERT INTO <DBTAB> [CLIENT SPECIFIED] VALUES <WA> . ``` ![](https://i.imgur.com/yAT5Th7.png) 實例 ``` data wa type zpu111_57_art. * 宣告一個wa * 如果忘記這個 table有什麼欄位 直接double click wa-artid = 'x001'. wa-artname = 'million'. wa-arttel = '0979223110'. wa-arturl = 'https://www.1.pu.edu.tw'. insert INTO zpu111_57_art VALUES wa. * 直接把這些資料寫到資料庫 if sy-subrc <> 0. * 如果subrc 小於大於0(不等於0) 表示一筆資料都沒寫進去 WRITE:/ '抱歉,新增失敗' . else. SELECT * from zpu111_57_art into wa. WRITE:/ wa-artid, wa-artname, wa-arttel, wa-arturl. ENDSELECT. ENDIF. * 只能新增一次 因為 artid設定為主鍵 必須為單一性 不能重複 ``` 實例- 讓使用者可以手動輸入 ``` PARAMETERS x1 type zpu111_57_art-artid. PARAMETERS x2 type zpu111_57_art-artname. PARAMETERS x3 type zpu111_57_art-arttel. PARAMETERS x4 type zpu111_57_art-arturl. START-OF-SELECTION. * 一次寫多筆資料 data wa type zpu111_57_art. * 宣告一個wa * 如果忘記這個 table有什麼欄位 直接double click wa-artid = x1. wa-artname = x2. wa-arttel = x3. wa-arturl = x4. * 如果輸入的時候不知道x1,x2是什麼,可以在textelement那裡修改 insert INTO zpu111_57_art VALUES wa. *一次寫多筆資料到資料庫 * 直接把這些資料寫到資料庫 if sy-subrc <> 0. * 如果subrc 小於大於0(不等於0) 表示一筆資料都沒寫進去 WRITE:/ '抱歉,新增失敗' . else. SELECT * from zpu111_57_art into wa. WRITE:/ wa-artid, wa-artname, wa-arttel, wa-arturl. ENDSELECT. ENDIF. * 只能新增一次 因為 artid是主鍵 必須為單一性 不能重複 ``` 輸出 ![](https://i.imgur.com/r63sYMC.png) ![](https://i.imgur.com/CSzYqA0.png) - Creating a Set of Records 一次寫入多筆資料 ``` INSERT <DBTAB> [CLIENT SPECIFIED] FROM TABLE <ITAB>. ``` ![](https://i.imgur.com/XjeGBVP.png) --- ### UPDATE 修改、變動 #### Changing a Single Record ![](https://i.imgur.com/1ErdIth.png) 實例 ``` * 要修改資料內容 假設改x001 data wa type zpu111_57_art. * 宣告一個wa * 如果忘記這個 table有什麼欄位 直接double click UPDATE zpu111_57_art set artname = 'lili' *這裡沒有句點 注意注意注意 WHERE artid = 'x001'. * 直接把這些資料寫到資料庫 if sy-subrc <> 0. * 如果subrc 小於大於0(不等於0) 表示一筆資料都沒寫進去 WRITE:/ '抱歉,修改失敗' . else. SELECT * from zpu111_57_art into wa. WRITE:/ wa-artid, wa-artname, wa-arttel, wa-arturl. ENDSELECT. ENDIF. ``` #### Changing Record Sets Via Conditions ![](https://i.imgur.com/517FMhd.png) #### Changing Record Sets Via Internal Tables ![](https://i.imgur.com/aiv2jor.png) #### Modifying Single Record and Sets 找不到資料就新增,找得到資料就修改 ![](https://i.imgur.com/IQpaDHs.png) 實例 ``` *modify 用法 data wa type zpu111_57_art. * 宣告wa 的 workarea * 如果忘記這個 table有什麼欄位 直接double click wa-artid = 'x006'. wa-artname = 'test3'. wa-arttel = '1111111111'. wa-arturl = 'https://www.1.pu.edu.tw'. MODIFY zpu111_57_art from wa. * 可以新增 可以修改 SELECT * from zpu111_57_art into wa. *把 wa的資料輸入到資料庫 zpu111_57_art裡面 WRITE:/ wa-artid, wa-artname, wa-arttel, wa-arturl. ENDSELECT. ``` --- ### DELETE 刪除 * Deleting a Single Record 刪一筆 ![](https://i.imgur.com/LODUA7I.png) 實例 ``` *delete 用法 刪除資料庫內容 data wa type zpu111_57_art. * 宣告wa 的 workarea * 如果忘記這個 table有什麼欄位 直接double click delete from zpu111_57_art where artid ='Y001'. * where條件決定一次刪多筆還是單筆 * 如果條件更改為>= 則y001 以及後面的編號都會被刪除 if sy-subrc <> 0. WRITE: / '沒有刪除資料。' . ELSE. SELECT * from zpu111_57_art into wa. *把 wa的資料輸入到資料庫 zpu111_57_art裡面 WRITE:/ wa-artid, wa-artname, wa-arttel, wa-arturl. ENDSELECT. ENDIF. ``` * Deleting Record Sets Via Conditions 刪多筆 ![](https://i.imgur.com/l7jXALC.png) * Deleting Record Sets Via Internal Tables ![](https://i.imgur.com/MwYrIlB.png)