当前位置:   article > 正文

S4HANA For ABAP(3):Internal Table改变_interal table

interal table

1.14 Internal Table:Formulating Dynamic Where Conditions

动态Where条件

 

 

7.02版本,可以在Loop时使用动态Where条件;

1.15 Internal Table:Defining and Using Secondary Keys

 

主要使用的Secondary Keys,使用Non-unique sorted keys;

示例代码:

  1. "test6
  2. "secondary key
  3. FORM f_test6.
  4. DATA:lt_spfli TYPE TABLE OF spfli
  5. WITH UNIQUE HASHED KEY k_hash COMPONENTS carrid connid countryfr
  6. WITH NON-UNIQUE SORTED KEY k_sort COMPONENTS carrid connid.
  7. SELECT * FROM spfli INTO TABLE lt_spfli.
  8. "loop时使用
  9. LOOP AT lt_spfli INTO DATA(ls_spfli) USING KEY k_sort WHERE carrid = 'AA' AND connid = '0001'.
  10. ENDLOOP.
  11. "read时使用
  12. READ TABLE lt_spfli INTO ls_spfli WITH KEY k_sort COMPONENTS carrid = 'AA' connid = '0001'.
  13. "当使用key读出记录需要更新时,必须使用对应key
  14. "sy-tabix记录的值会被key影响
  15. "更新时需要指定transporting字段,不能更新key值字段
  16. MODIFY lt_spfli USING KEY k_sort FROM ls_spfli INDEX sy-tabix
  17. TRANSPORTING countryfr.
  18. ENDFORM. 

1.16 Internal Table:Processing Internal Tables Using Expressions

示例代码:

  1. "test7
  2. "表达式访问内表
  3. FORM f_test7.
  4. DATA:lt_spfli TYPE TABLE OF spfli
  5. WITH NON-UNIQUE SORTED KEY k_sort
  6. COMPONENTS countryfr.
  7. DATA:ls_spfli LIKE LINE OF lt_spfli.
  8. DATA:lv_countryfr TYPE spfli-countryfr.
  9. SELECT * FROM spfli INTO TABLE lt_spfli.
  10. "line index从1开始
  11. ls_spfli = lt_spfli[ 1 ].
  12. WRITE:/ ls_spfli-carrid, ls_spfli-connid.
  13. "匹配多笔,只返回第一笔记录
  14. ls_spfli = lt_spfli[ carrid = 'AA' ].
  15. WRITE:/ ls_spfli-carrid, ls_spfli-connid.
  16. "使用key
  17. ls_spfli = lt_spfli[ KEY k_sort COMPONENTS countryfr = 'DE' ].
  18. WRITE:/ ls_spfli-carrid, ls_spfli-connid.
  19. "访问结构字段
  20. lv_countryfr = lt_spfli[ carrid = 'AA' ]-countryfr.
  21. "通过line index访问行
  22. * READ TABLE lt_spfli WITH KEY k_sort COMPONENTS countryfr = 'DE' TRANSPORTING NO FIELDS.
  23. * LOOP AT lt_spfli INTO ls_spfli FROM sy-tabix.
  24. * ENDLOOP.
  25. "line_index()方法
  26. "line_exists()方法
  27. "需要line_exists先判断访问数据行是否存在,
  28. "再使用line_index获取数据行
  29. IF line_exists( lt_spfli[ KEY k_sort COMPONENTS countryfr = 'DE' ] ).
  30. LOOP AT lt_spfli INTO ls_spfli
  31. FROM line_index( lt_spfli[ KEY k_sort COMPONENTS countryfr = 'DE' ] ).
  32. ENDLOOP.
  33. ENDIF.
  34. ENDFORM.

1.17 Internal Table:Creating Comprehensions And Reductions

简化内表循环处理;

数据汇总方法;

示例代码:

  1. "test8
  2. "Creating Comprehensions And Reductions
  3. FORM f_test8.
  4. DATA:lt_spfli TYPE TABLE OF spfli
  5. WITH NON-UNIQUE SORTED KEY s_sort COMPONENTS countryfr.
  6. DATA:lt_spfli2 TYPE TABLE OF spfli.
  7. DATA:ls_spfli LIKE LINE OF lt_spfli.
  8. TYPES:BEGIN OF s_ty1,
  9. carrid TYPE spfli-carrid,
  10. connid TYPE spfli-connid,
  11. END OF s_ty1.
  12. DATA:lt_ty1 TYPE TABLE OF s_ty1.
  13. DATA:ls_ty1 LIKE LINE OF lt_ty1.
  14. SELECT * FROM spfli INTO TABLE lt_spfli.
  15. "使用MOVE-CORRESPONDING进行内表赋值
  16. MOVE-CORRESPONDING lt_spfli TO lt_ty1.
  17. "保留lt_ty1已有记录
  18. "MOVE-CORRESPONDING lt_spfli TO lt_ty1 KEEPING TARGET LINES.
  19. "如果有嵌套结构赋值
  20. "MOVE-CORRESPONDING lt_spfli TO lt_ty1 EXPANDING NESTED TABLES.
  21. "不使用Loop,获取内表部分行
  22. lt_spfli2 = VALUE #( FOR wa IN lt_spfli
  23. WHERE ( carrid = 'AA' )
  24. ( wa ) ).
  25. "结合动态where条件,注意where两边括号不能有空格
  26. DATA:lv_carrid TYPE string VALUE 'AZ'.
  27. DATA:lv_connid TYPE string VALUE '0788'.
  28. DATA:lv_where TYPE string.
  29. lv_where = | CARRID = '{ lv_carrid }' AND CONNID = '{ lv_connid }' |.
  30. WRITE:/ lv_where.
  31. lt_spfli2 = VALUE #( FOR wa IN lt_spfli
  32. WHERE (lv_where)
  33. ( wa ) ).
  34. LOOP AT lt_spfli2 INTO DATA(ls_spfli2).
  35. WRITE:/ ls_spfli2-carrid,ls_spfli2-connid.
  36. ENDLOOP.
  37. "通过REDUCE,进行数据汇总
  38. DATA:lt_sflight TYPE TABLE OF sflight.
  39. DATA:ls_sflight LIKE LINE OF lt_sflight.
  40. TYPES:BEGIN OF s_ty2,
  41. carrid TYPE sflight-carrid,
  42. connid TYPE sflight-connid,
  43. sum_occ TYPE i,
  44. END OF s_ty2.
  45. DATA:lt_sflight2 TYPE TABLE OF s_ty2.
  46. DATA:lt_sflight3 TYPE TABLE OF s_ty2.
  47. DATA:lv_occ_total TYPE I.
  48. SELECT * FROM sflight INTO TABLE lt_sflight.
  49. "加总所有seatsocc
  50. lv_occ_total = REDUCE i( INIT i = 0
  51. FOR ls_sflight2 IN lt_sflight
  52. NEXT i = i + ls_sflight2-seatsocc
  53. ).
  54. "根据carrid,connid分组汇总seatsocc
  55. LOOP AT lt_sflight INTO ls_sflight
  56. GROUP BY ( carrid = ls_sflight-carrid
  57. connid = ls_sflight-connid
  58. size = GROUP SIZE )
  59. ASCENDING ASSIGNING FIELD-SYMBOL(<fs_sflight>).
  60. lt_sflight2 = VALUE #( (
  61. carrid = <fs_sflight>-carrid
  62. connid = <fs_sflight>-connid
  63. sum_occ = REDUCE i( INIT i = 0
  64. FOR line IN GROUP <fs_sflight>
  65. NEXT i = i + line-seatsocc ) )
  66. ).
  67. MOVE-CORRESPONDING lt_sflight2 TO lt_sflight3 KEEPING TARGET LINES.
  68. ENDLOOP.
  69. LOOP AT lt_sflight3 ASSIGNING FIELD-SYMBOL(<fs_sflight3>).
  70. WRITE:/ <fs_sflight3>-carrid,<fs_sflight3>-connid,<fs_sflight3>-sum_occ.
  71. ENDLOOP.
  72. ENDFORM. 

1.18 Internal Table:Defining and Using Meshes

通过定义Table之间外键关系,实现多表连接,减少访问时循环次数。

但是查询时,会将多表数据同时查询出来,是否会出现内存溢出

示例:

  1. "test9
  2. "Defining and Using Meshes
  3. "语法:
  4. *TYPES BEGIN OF MESH mesh_type.
  5. * node { TYPE {[REF TO] table_type}|ref_type }
  6. * | { LIKE {[REF TO] itab }|dref }
  7. * [association1],
  8. * [association2],
  9. * ...
  10. *TYPES END OF MESH mesh_type.
  11. "示例:DEMO_MESH*
  12. "Mesh Node
  13. "语法:
  14. "{mesh-|<mesh>-|mesh_ref->}rnode\_associ[ ... ]\_assoc1[ ... ]\_assoc2[ ... ]
  15. "1.{mesh-|<mesh>-|mesh_ref->}rnode
  16. "rnode是mesh中定义的node,可以作为开始node或结束node;
  17. "2. \_associ[ source [cond] ]
  18. "通过association获取对应结果node数据,可以指定开始node数据筛选条件;
  19. "3.\_assoc1[ [cond] ]\_assoc2[ [cond] ]
  20. "可以链式使用association访问数据;
  21. "Mesh Association
  22. "语法:
  23. "\_assoc[+|*][ ... ]
  24. "| \_assoc~node[+|*][ ... ]
  25. "| \^_assoc~node[+|*][ ... ] ...
  26. "示例:DEMO_MESH_REFLEXIVE_ASSOC_SNGL
  27. "+,*主要用于自关联表树状关系
  28. "+,获取开始node下所有子孙节点数据;
  29. "*,包含开始node匹配数据,获取开始node下所有子孙节点数据;
  30. "例如:
  31. "课程ID 上阶课程 描述
  32. "ID PID DESC
  33. "1 0 t1
  34. "2 1 t2
  35. "3 2 t3
  36. "4 0 t4
  37. "5 1 t5
  38. *TYPES:BEGIN OF line,
  39. * id TYPE i,
  40. * pid TYPE i,
  41. * desc TYPE string,
  42. * END OF line.
  43. *TYPES:t_itab TYPE SORTED TABLE OF line WITH UNIQUE KEY id
  44. * WITH NON-UNIQUE SORTED KEY by_parent
  45. * COMPONENTS pid,
  46. *TYPES:BEGIN OF MESH t_mesh,
  47. * node TYPE t_itab
  48. * ASSOCIATION _node TO node ON pid = id
  49. * USING KEY by_parent,
  50. * END OF MESH t_mesh.
  51. "需要查找所有课程ID为1的下阶课程
  52. "mesh-node\_node[ mesh-node[ id = id ] ]
  53. "返回:t2,t5两笔记录
  54. "mesh-node\_node+[ mesh-node[ id = id ] ]
  55. "返回:t2,t5,t3, t3返回因为他是孙子课程
  56. "mesh-node\_node*[ mesh-node[ id = id ] ]
  57. "返回:t2,t5,t3,t1 t1返回因为它符合初始条件
  58. "Mesh Condition
  59. "语法:
  60. "{ col1 = val1 col2 = val2 ... }
  61. "| { [USING KEY key] [WHERE log_exp] }
  62. "设置筛选条件
  63. "[USING KEY key] [WHERE log_exp]
  64. "可以在下面语法中使用:
  65. "LOOP AT mesh_path ...
  66. "FOR ... IN mesh_path ...
  67. "INSERT ... INTO TABLE mesh_path ...
  68. "MODIFY mesh_path.
  69. "DELETE mesh_path.
  70. "SET ASSOCIATION mesh_path ...
  71. FORM f_test9.
  72. "定义MESH
  73. "不能使用standard table
  74. "TYPES:tt_customers TYPE TABLE OF scustom.
  75. "TYPES:tt_customers TYPE STANDARD TABLE OF scustom.
  76. "TYPES:tt_customers TYPE STANDARD TABLE OF scustom
  77. " WITH NON-UNIQUE SORTED KEY k_sort COMPONENTS id name.
  78. "需要使用sorted table,hash table
  79. TYPES:tt_customers TYPE SORTED TABLE OF scustom WITH NON-UNIQUE KEY id name.
  80. TYPES:tt_books TYPE HASHED TABLE OF sbook WITH UNIQUE KEY carrid connid fldate bookid.
  81. TYPES:wa_books TYPE LINE OF tt_books.
  82. "定义mesh
  83. TYPES:BEGIN OF MESH m_custom_books,
  84. customers TYPE tt_customers ASSOCIATION _sbook TO books ON customid = id,
  85. books TYPE tt_books ASSOCIATION _scutomer TO customers ON id = customid,
  86. END OF MESH m_custom_books.
  87. "也可以定义多种key
  88. "SORT,HASH的UNIQUE KEY必须要指定
  89. TYPES:tt_scarr TYPE HASHED TABLE OF scarr
  90. WITH UNIQUE KEY carrid.
  91. TYPES:tt_spfli TYPE HASHED TABLE OF spfli
  92. WITH UNIQUE KEY carrid connid
  93. WITH NON-UNIQUE SORTED KEY k_sort_spfli COMPONENTS carrid.
  94. TYPES:tt_sflight TYPE HASHED TABLE OF sflight
  95. WITH UNIQUE KEY carrid connid fldate
  96. WITH NON-UNIQUE SORTED KEY k_sort_sflight COMPONENTS carrid connid.
  97. TYPES:tt_sairport TYPE HASHED TABLE OF sairport
  98. WITH UNIQUE KEY id.
  99. "定义mesh
  100. TYPES:BEGIN OF MESH m_flights_info,
  101. scarrs TYPE tt_scarr
  102. ASSOCIATION _spfli TO spflis
  103. ON carrid = carrid USING KEY k_sort_spfli,
  104. spflis TYPE tt_spfli
  105. "和sflight直接association
  106. ASSOCIATION _sflight TO sflights
  107. ON carrid = carrid
  108. AND connid = connid USING KEY k_sort_sflight
  109. "和airport之间association
  110. ASSOCIATION _sairport TO sairports
  111. ON id = airpto,
  112. "USING KEY primary_key,
  113. sflights TYPE tt_sflight,
  114. sairports TYPE tt_sairport,
  115. END OF MESH m_flights_info.
  116. "指针方式
  117. DATA:lm_custom_books3_ref TYPE REF TO DATA.
  118. FIELD-SYMBOLS:<fs_custom_books> TYPE m_custom_books.
  119. "创建mesh对象
  120. "方式1
  121. DATA:lm_custom_books TYPE m_custom_books.
  122. DATA:lm_flights_info TYPE m_flights_info.
  123. ASSIGN lm_custom_books TO <fs_custom_books>.
  124. "方式2
  125. DATA(lm_custom_books2) = NEW m_custom_books( ).
  126. DATA(lm_custom_books2_ref) = NEW m_custom_books( VALUE m_custom_books( ) ).
  127. lm_custom_books3_ref = REF #( lm_custom_books2 ).
  128. "填充mesh数据,这样数据过多同样会有内存限制?
  129. SELECT * FROM scustom INTO TABLE lm_custom_books-customers.
  130. SELECT * FROM sbook INTO TABLE @lm_custom_books-books.
  131. SELECT * FROM scarr INTO TABLE @lm_flights_info-scarrs.
  132. SELECT * FROM spfli INTO TABLE @lm_flights_info-spflis.
  133. SELECT * FROM sflight INTO TABLE @lm_flights_info-sflights.
  134. SELECT * FROM sairport INTO TABLE @lm_flights_info-sairports.
  135. "正反向数据访问
  136. "正向Forward associations,root node作为开始node;
  137. "反向Inverse associations,root node作为结束node;
  138. "正向数据访问
  139. "cutomers\_sbook
  140. "lt_result1数据类型和最后一个association对应表一致
  141. "根据customer表查询book表信息
  142. "lm_custom_books-customers\_sbook
  143. IF line_exists( lm_custom_books-customers\_sbook[
  144. lm_custom_books-customers[ id = '00001134' ] ] ).
  145. DATA(lt_result1) = VALUE tt_books( ( lm_custom_books-customers\_sbook[
  146. lm_custom_books-customers[ id = '00001134' ]
  147. ] ) ).
  148. "输出显示
  149. LOOP AT lt_result1 ASSIGNING FIELD-SYMBOL(<fs_result1>).
  150. WRITE:/ 'result1:',<fs_result1>-passname,<fs_result1>-order_date.
  151. ENDLOOP.
  152. ENDIF.
  153. "反向数据访问
  154. "lt_result2数据类型和~后的表类型一致
  155. "根据book表信息查询customer信息
  156. "lm_custom_books-books\^_sbook~customers
  157. IF line_exists( lm_custom_books-books\^_sbook~customers[
  158. lm_custom_books-books[ customid = '00001134' ] ] ).
  159. DATA(lt_result2) = VALUE tt_customers( ( lm_custom_books-books\^_sbook~customers[
  160. lm_custom_books-books[ customid = '00001134' ]
  161. ] ) ).
  162. "输出显示
  163. LOOP AT lt_result2 ASSIGNING FIELD-SYMBOL(<fs_result2>).
  164. WRITE:/ 'result2:',<fs_result2>-name,<fs_result2>-city.
  165. ENDLOOP.
  166. ENDIF.
  167. "根据customer表信息查询book信息
  168. "lm_custom_books-customers\^_scutomer~books
  169. IF line_exists( lm_custom_books-customers\^_scutomer~books[
  170. lm_custom_books-customers[ id = '00001134' ] ] ).
  171. "VALUE中使用()表示返回table类型,
  172. "注意:如果匹配多笔,也只返回单笔记录!
  173. DATA(lt_result3) = VALUE tt_books( ( lm_custom_books-customers\^_scutomer~books[
  174. lm_custom_books-customers[ id = '00001134' ] ] ) ).
  175. "VALUE中不使用(),表示返回工作区,单笔记录
  176. DATA(lt_result4) = VALUE wa_books( lm_custom_books-customers\^_scutomer~books[
  177. lm_custom_books-customers[ id = '00001134' ] ] ).
  178. ENDIF.
  179. LOOP AT lt_result3 ASSIGNING FIELD-SYMBOL(<fs_result3>).
  180. WRITE:/ 'result3:',<fs_result3>-passname,<fs_result3>-order_date.
  181. ENDLOOP.
  182. WRITE:/ 'result4:',lt_result4-passname,lt_result4-order_date.
  183. "多层级正向association
  184. "lm_flights_info-scarrs\_spfli[]\_sflight[ ]
  185. IF line_exists( lm_flights_info-scarrs\_spfli[
  186. lm_flights_info-scarrs[ carrid = 'AA' ] ]\_sflight[ ] ).
  187. "有多笔sflight记录,只返回单笔
  188. DATA(lt_result5) = VALUE tt_sflight( ( lm_flights_info-scarrs\_spfli[
  189. lm_flights_info-scarrs[ carrid = 'AA' ] ]\_sflight[ ] ) ).
  190. "返回多笔记录
  191. DATA(lt_result6) = VALUE tt_sflight( FOR <fs_result6> IN lm_flights_info-scarrs\_spfli[
  192. lm_flights_info-scarrs[ carrid = 'AA' ] ]\_sflight[ ]
  193. ( <fs_result6> )
  194. ).
  195. ENDIF.
  196. LOOP AT lt_result5 ASSIGNING FIELD-SYMBOL(<fs_result5>).
  197. WRITE:/ 'result5:',<fs_result5>-carrid,<fs_result5>-connid,<fs_result5>-fldate.
  198. ENDLOOP.
  199. "因为上面<fs_result6>已经被定义使用,
  200. "这里不能使用field symbol(<fs_result6>)
  201. "也不能直接使用fs_result6?
  202. "LOOP AT lt_result6 ASSIGNING FIELD-SYMBOL(<fs_result6>).
  203. "LOOP AT lt_result6 ASSIGNING <fs_result6>.
  204. LOOP AT lt_result6 ASSIGNING FIELD-SYMBOL(<fs_result61>).
  205. WRITE:/ 'result6:',<fs_result61>-carrid,<fs_result61>-connid,<fs_result61>-fldate.
  206. ENDLOOP.
  207. "多层级反向association
  208. "lm_flights_info-sflights\^_sflight~spflis[]\^_spfli~scarrs[]
  209. IF line_exists( lm_flights_info-sflights\^_sflight~spflis[
  210. lm_flights_info-sflights[ carrid = 'AA' ] ]\^_spfli~scarrs[ ] ).
  211. DATA(lt_result7) = VALUE tt_scarr( FOR <fs_result7> IN lm_flights_info-sflights\^_sflight~spflis[
  212. lm_flights_info-sflights[ carrid = 'AA' ] ]\^_spfli~scarrs[ ]
  213. ( <fs_result7> ) ).
  214. "使用field symbol保存结果
  215. "注意:这里返回是工作区类型
  216. ASSIGN lm_flights_info-sflights\^_sflight~spflis[
  217. lm_flights_info-sflights[ carrid = 'AA' ] ]\^_spfli~scarrs[ ]
  218. TO FIELD-SYMBOL(<fs_result72>).
  219. ENDIF.
  220. LOOP AT lt_result7 ASSIGNING FIELD-SYMBOL(<fs_result71>).
  221. WRITE:/ 'result7:',<fs_result71>-carrid,<fs_result71>-carrname,<fs_result71>-url.
  222. ENDLOOP.
  223. WRITE:/ 'result72:',<fs_result72>-carrid,<fs_result72>-carrname,<fs_result72>-url.
  224. "Loop AT 语句
  225. "LOOP AT mesh_path result.
  226. "...
  227. "ENDLOOP.
  228. LOOP AT lm_flights_info-spflis\_sflight[
  229. lm_flights_info-spflis[ carrid = 'AA' connid = '0064' ]
  230. WHERE fldate = '20220423'
  231. "AND currency = 'USD'
  232. ]
  233. INTO DATA(ls_sflight).
  234. WRITE:/ 'Loop at:',ls_sflight-connid,ls_sflight-fldate,ls_sflight-price.
  235. ENDLOOP.
  236. "注意:
  237. "lm_flights_info-spflis[ carrid = 'AA' ]时
  238. "table:spfli有两笔匹配记录,connid = '0017' or '0064'
  239. "但只会返回第一笔数据carrid = 'AA' connid = '0017'
  240. "然后额外条件WHERE connid = '0064'时不会返回任何记录;
  241. LOOP AT lm_flights_info-spflis\_sflight[
  242. lm_flights_info-spflis[ carrid = 'AA' connid = '0064' ]
  243. "lm_flights_info-spflis[ carrid = 'AA' ]
  244. "WHERE connid = '0064'
  245. ]
  246. INTO DATA(ls_sflight1).
  247. WRITE:/ 'Loop at1:',ls_sflight1-connid,ls_sflight1-fldate,ls_sflight1-price.
  248. ENDLOOP.
  249. "lm_flights_info-scarrs\_spfli[]\_sairport[]
  250. LOOP AT lm_flights_info-scarrs\_spfli[
  251. lm_flights_info-scarrs[ carrname = 'Alitalia' ]
  252. WHERE countryto = 'JP'
  253. ]\_sairport[ USING KEY primary_key ]
  254. INTO DATA(ls_sairport).
  255. "INTO DATA(ls_spfli).
  256. WRITE:/ 'Loop at2:',ls_sairport-id,ls_sairport-name.
  257. "WRITE:/ 'Loop at2:',ls_spfli-carrid,ls_spfli-connid.
  258. ENDLOOP.
  259. "FOR IN语句
  260. "FOR wa|<fs> IN mesh_path [let_exp]
  261. DATA:lt_sflights TYPE tt_sflight.
  262. lt_sflights = VALUE tt_sflight(
  263. FOR ls_sflights IN lm_flights_info-scarrs\_spfli[
  264. lm_flights_info-scarrs[ carrname = 'American Airlines' ] ]\_sflight[ ]
  265. ( ls_sflights )
  266. ).
  267. "输出结果
  268. LOOP AT lt_sflights INTO DATA(ls_sflights2).
  269. WRITE:/ 'FOR IN:',ls_sflights2-carrid,ls_sflights2-connid,ls_sflights2-fldate.
  270. ENDLOOP.
  271. "结合REDUCE汇总
  272. DATA(lv_sum) = REDUCE sflight-paymentsum(
  273. INIT v_sum TYPE sflight-paymentsum
  274. FOR ls_sflights3 IN lm_flights_info-scarrs\_spfli[
  275. lm_flights_info-scarrs[ carrname = 'American Airlines' ] ]\_sflight[ ]
  276. NEXT v_sum = v_sum + ls_sflights3-paymentsum
  277. ).
  278. WRITE:/ 'FOR IN2:',lv_sum.
  279. "定义mesh
  280. TYPES:BEGIN OF s_line1,
  281. id TYPE i,
  282. desc TYPE string,
  283. END OF s_line1.
  284. TYPES:BEGIN OF s_line2,
  285. id TYPE i,
  286. name TYPE string,
  287. desc TYPE string,
  288. END OF s_line2.
  289. TYPES:tt_line1 TYPE SORTED TABLE OF s_line1
  290. WITH NON-UNIQUE KEY id.
  291. TYPES:tt_line2 TYPE SORTED TABLE OF s_line2
  292. WITH NON-UNIQUE KEY id
  293. WITH NON-UNIQUE SORTED KEY k_1 COMPONENTS id name.
  294. TYPES:BEGIN OF MESH m_line,
  295. line1 TYPE tt_line1
  296. ASSOCIATION _line2 TO line2 ON id = id,
  297. line2 TYPE tt_line2,
  298. END OF MESH m_line.
  299. "INSERT语句
  300. "INSERT line_spec INTO TABLE mesh_path result.
  301. "插入时,如果UNIQUE Key不能插入重复值
  302. DATA:lm_line TYPE m_line.
  303. lm_line-line1 = VALUE #(
  304. ( id = 1 desc = 'line11' )
  305. ( id = 2 desc = 'line12' )
  306. ( id = 3 desc = 'line13' )
  307. ).
  308. lm_line-line2 = VALUE #(
  309. ( id = 1 desc = 'line21' )
  310. ( id = 2 desc = 'line22' )
  311. ( id = 2 desc = 'line222' )
  312. ).
  313. INSERT VALUE s_line1( desc = 'line14' ) INTO TABLE lm_line-line1.
  314. INSERT LINES OF VALUE tt_line1(
  315. ( id = 5 desc = 'line15' )
  316. ( desc = 'line16' )
  317. ) INTO TABLE lm_line-line1.
  318. INSERT INITIAL LINE INTO TABLE lm_line-line1.
  319. DATA:ls_line1 LIKE LINE OF lm_line-line1.
  320. LOOP AT lm_line-line1 INTO ls_line1.
  321. WRITE:/ 'Insert:',ls_line1-id,ls_line1-desc.
  322. ENDLOOP.
  323. INSERT VALUE s_line2( name = 'name1' desc = 'line24' ) INTO TABLE lm_line-line1\_line2[
  324. lm_line-line1[ 1 ] ].
  325. DATA:ls_line2 LIKE LINE OF lm_line-line2.
  326. LOOP AT lm_line-line2 INTO ls_line2.
  327. WRITE:/ 'Insert:',ls_line2-id,ls_line2-name,ls_line2-desc.
  328. ENDLOOP.
  329. "Modify语句
  330. "MODIFY { TABLE mesh_path [USING KEY keyname] FROM wa
  331. " [TRANSPORTING comp1 comp2 ...] result }
  332. " | { mesh_path FROM wa [TRANSPORTING comp1 comp2 ...] }.
  333. "MODIFY TABLE只修改符合条件单笔
  334. MODIFY TABLE lm_line-line1\_line2[
  335. lm_line-line1[ id = 2 ] ]
  336. FROM VALUE s_line2( name = 'modify1' desc = 'line66' ).
  337. MODIFY TABLE lm_line-line1\_line2[
  338. lm_line-line1[ id = 0 ] ]
  339. FROM VALUE s_line2( name = 'modify2' )
  340. TRANSPORTING name.
  341. "输出显示
  342. LOOP AT lm_line-line2 INTO ls_line2.
  343. WRITE:/ 'Modify:',ls_line2-id,ls_line2-name,ls_line2-desc.
  344. ENDLOOP.
  345. "MODIFY 修改符合条件多笔
  346. MODIFY lm_line-line1\_line2[
  347. lm_line-line1[ id = 2 ] ]
  348. FROM VALUE s_line2( name = 'modify multi' )
  349. TRANSPORTING name.
  350. "输出显示
  351. LOOP AT lm_line-line2 INTO ls_line2.
  352. WRITE:/ 'Modify:',ls_line2-id,ls_line2-name,ls_line2-desc.
  353. ENDLOOP.
  354. "Delete语句
  355. "DELETE { TABLE mesh_path table_key } | { mesh_path }.
  356. * "删除单笔记录
  357. * "1.删除line1中第4行id=1对应line2中id=1单行数据
  358. * DELETE TABLE lm_line-line1\_line2[
  359. * lm_line-line1[ 4 ] ].
  360. * "输出显示
  361. * LOOP AT lm_line-line2 INTO ls_line2.
  362. * WRITE:/ 'DELETE:',ls_line2-id,ls_line2-name,ls_line2-desc.
  363. * ENDLOOP.
  364. * "2.使用TABLE KEY删除
  365. * DELETE TABLE lm_line-line1\_line2[
  366. * lm_line-line1[ 5 ] ]
  367. * WITH TABLE KEY k_1 COMPONENTS name = 'modify multi'.
  368. * "输出显示
  369. * LOOP AT lm_line-line2 INTO ls_line2.
  370. * WRITE:/ 'DELETE1:',ls_line2-id,ls_line2-name,ls_line2-desc.
  371. * ENDLOOP.
  372. * "3.使用VALUE指定行域值删除
  373. * DELETE TABLE lm_line-line1\_line2[
  374. * lm_line-line1[ 5 ] ]
  375. * FROM VALUE s_line2( name = 'modify multi' )
  376. * USING KEY k_1.
  377. "删除多笔记录
  378. "1.line15行id = 2,删除line2中所有id=2记录
  379. * DELETE lm_line-line1\_line2[
  380. * lm_line-line1[ 5 ] ].
  381. "2.where条件删除
  382. DELETE lm_line-line1\_line2[
  383. lm_line-line1[ 5 ]
  384. WHERE desc = 'line22' ].
  385. "通过sy-subrc判断是否删除成功
  386. IF sy-subrc = 0.
  387. WRITE:/ 'delete success'.
  388. ELSE.
  389. WRITE:/ 'delete failed'.
  390. ENDIF.
  391. "定义mesh
  392. TYPES:BEGIN OF s_1,
  393. col1 TYPE i,
  394. col2 TYPE i,
  395. END OF s_1.
  396. TYPES:BEGIN OF s_2,
  397. col1 TYPE i,
  398. col2 TYPE i,
  399. col3 TYPE i,
  400. col4 TYPE i,
  401. END OF s_2.
  402. TYPES:BEGIN OF s_3,
  403. col3 TYPE i,
  404. col4 TYPE i,
  405. END OF s_3.
  406. TYPES:tt_1 TYPE SORTED TABLE OF s_1
  407. WITH NON-UNIQUE KEY col1 col2.
  408. TYPES:tt_2 TYPE SORTED TABLE OF s_2
  409. WITH NON-UNIQUE KEY col1 col2.
  410. TYPES:tt_3 TYPE SORTED TABLE OF s_3
  411. WITH NON-UNIQUE KEY col3.
  412. TYPES:BEGIN OF MESH m_1,
  413. t1 TYPE tt_1
  414. ASSOCIATION _t2 TO t2 ON col1 = col1 AND col2 = col2,
  415. t2 TYPE tt_2
  416. ASSOCIATION _t3 TO t3 ON col3 = col3 AND col4 = col4,
  417. t3 TYPE tt_3,
  418. END OF MESH m_1.
  419. "SET ASSOCIATION语句
  420. "SET ASSOCIATION mesh_path { = wa } | { LIKE wa } | { INITIAL }.
  421. "只会将association对应域值赋值给前置表对应字段
  422. "如果col4没有在association条件,则值不会写入
  423. DATA:lm_1 TYPE m_1.
  424. lm_1-t1 = VALUE #(
  425. ( col1 = 1 col2 = 11 )
  426. ( col1 = 2 col2 = 12 ) ).
  427. lm_1-t2 = VALUE #(
  428. ( col1 = 1 col2 = 11 )
  429. ( col1 = 2 col2 = 222 ) ).
  430. lm_1-t3 = VALUE #(
  431. ( col3 = 31 col4 = 331 )
  432. ( col3 = 32 col4 = 332 ) ).
  433. "set association
  434. "方式1:根据assiciation:_t3设置t2的col3,col4
  435. "这句设置不成功?
  436. "只能初始化设置值然后需要带出association值时使用?
  437. "DATA(ls_2) = VALUE s_2( col1 = 1 col2 = 11 ).
  438. "SET ASSOCIATION lm_1-t2\_t3[ ls_2 ] = lm_1-t3[ 2 ].
  439. INSERT INITIAL LINE INTO TABLE
  440. lm_1-t1\_t2[ VALUE s_1( col1 = 1 col2 = 12 ) ]
  441. ASSIGNING FIELD-SYMBOL(<line2>).
  442. SET ASSOCIATION lm_1-t2\_t3[ <line2> ] = lm_1-t3[ 1 ].
  443. "方式2:从节点1开始,实现相同效果
  444. "根据assiciation:_t3设置t2的col3,col4
  445. SET ASSOCIATION lm_1-t1\_t2[ VALUE s_1( col1 = 1 col2 = 11 ) ]\_t3[ ] = lm_1-t3[ 2 ].
  446. "SET ASSOCIATION mesh_path LIKE wa
  447. INSERT INITIAL LINE INTO TABLE
  448. lm_1-t1\_t2[ VALUE s_1( col1 = 1 col2 = 13 ) ]
  449. ASSIGNING FIELD-SYMBOL(<line22>).
  450. SET ASSOCIATION lm_1-t2\_t3[ <line22> ] LIKE VALUE s_2( col3 = 333 col4 = 3334 ).
  451. ENDFORM.
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/84687
推荐阅读
相关标签
  

闽ICP备14008679号