學習軌道
/
ABAP
ABAP
/
練習
/
ITAB 巢狀
ITAB 巢狀

ITAB 巢狀

簡單

說明

學習如何將三個不同內部資料表的資料合併成一個結果內部資料表,同時把相關聯的資料巢狀在一起。

你的類別會拿到三個內部資料表,裡面分別是三組完全不同的資料集:ARTISTS、ALBUMS 和 SONGS。

TYPES: BEGIN OF artists_type,
         artist_id   TYPE string,
         artist_name TYPE string,
       END OF artists_type.
TYPES: artists TYPE STANDARD TABLE OF artists_type WITH KEY artist_id.
TYPES: BEGIN OF albums_type,
         artist_id  TYPE string,
         album_id   TYPE string,
         album_name TYPE string,
       END OF albums_type.
TYPES: albums TYPE STANDARD TABLE OF albums_type WITH KEY artist_id album_id.
TYPES: BEGIN OF songs_type,
         artist_id TYPE string,
         album_id  TYPE string,
         song_id   TYPE string,
         song_name TYPE string,
       END OF songs_type.
TYPES: songs TYPE STANDARD TABLE OF songs_type WITH KEY artist_id album_id song_id.

ARTISTS 資料表中的資料如下所示。

ARTIST_ID ARTIST_NAME
1 Godsmack
2 Shinedown

ALBUMS 資料表中的資料如下所示。

ARTIST_ID ALBUM_ID ALBUM_NAME
1 1 Faceless
1 2 When Lengends Rise
2 1 The Sound of Madness
2 2 Planet Zero

SONGS 資料表中的資料如下所示。

ARTIST_ID ALBUM_ID SONG_ID SONG_NAME
1 1 1 Straight Out Of Line
1 1 2 Changes
1 2 1 Bullet Proof
1 2 2 Under Your Scars
2 1 1 Second Chance
2 1 2 Breaking Inside
2 2 1 Dysfunctional You
2 2 2 Daylight

你的任務是回傳一個內部資料表,其中的記錄以結構化的方式合併每個內部資料表的值,把 SONGS 內部資料表巢狀進 ALBUMS 內部資料表,當然也要把 ALBUMS 內部資料表巢狀進 ARTISTS 內部資料表。

預期回傳的資料表定義如下:

TYPES: BEGIN OF song_nested_type,
         song_id   TYPE string,
         song_name TYPE string,
       END OF song_nested_type.
TYPES: BEGIN OF album_song_nested_type,
         album_id   TYPE string,
         album_name TYPE string,
         songs      TYPE STANDARD TABLE OF song_nested_type WITH KEY song_Id,
       END OF album_song_nested_type.
TYPES: BEGIN OF artist_album_nested_type,
         artist_id   TYPE string,
         artist_name TYPE string,
         albums      TYPE STANDARD TABLE OF album_song_nested_type WITH KEY album_id,
       END OF artist_album_nested_type.
TYPES: nested_data TYPE STANDARD TABLE OF artist_album_nested_type WITH KEY artist_id.

加分題

在 ABAP 中有很多方法可以完成這項任務。你能多有效率地完成它呢?

透過 GitHub 編輯 連結會在新視窗或分頁中開啟
ABAP Exercism

準備好開始 ITAB 巢狀 了嗎?

註冊 Exercism,透過 54 個練習 和真人引導來學習並精通 ABAP,全部免費。