二维码

复制迁移数据库表数据程序

Twilight发表于 2015-05-26 21:32zhongguomao 最后回复于 2017-08-28 15:19 [复制链接] 2705 1

其实很简单就是用move-corresponding语句,匹配两边的相同字段,然后赋值,就可以实现简单地在数据库表之间迁移数据了。
  1. *&---------------------------------------------------------------------*
  2. *& Report  ZTABLECOPY
  3. *&
  4. *&---------------------------------------------------------------------*
  5. *&
  6. *&
  7. *&---------------------------------------------------------------------*

  8. REPORT  ZTABLECOPY.
  9. parameters:p_stbl type tabname16,"Source table name
  10.            p_ttbl type tabname16,"Target table name
  11.            p_over as checkbox,"Overwrite existing data
  12.            p_del as checkbox."Delete before copy
  13. data:record_count type int4.
  14. data:gs_table type dd02l.

  15. at selection-screen on p_stbl."Check existence of source table
  16.   select single *
  17.     from dd02l
  18.     into gs_table
  19.     where tabname = p_stbl
  20.       and as4local = 'A'.
  21.   if sy-subrc ne 0.
  22.     message 'Source table not exists' type 'E'.
  23.   endif.

  24. at selection-screen on p_ttbl."Check existence of target table
  25.   select single *
  26.     from dd02l
  27.     into gs_table
  28.     where tabname = p_ttbl
  29.       and as4local = 'A'.
  30.   if sy-subrc ne 0.
  31.     message 'Target table not exists' type 'E'.
  32.   endif.

  33.   select count(*) into record_count from (p_ttbl).
  34.     if record_count gt 0 and p_over is initial and p_del is initial.
  35.       message 'Target table has data' type 'E'.
  36.     endif.

  37. data:gt_data_s type ref to data,
  38.      gs_data_s type ref to data,
  39.      go_struct_type_s type ref to cl_abap_structdescr,
  40.      go_table_type_s type ref to cl_abap_tabledescr,
  41.      gt_data_t type ref to data,
  42.      gs_data_t type ref to data,
  43.      go_struct_type_t type ref to cl_abap_structdescr,
  44.      go_table_type_t type ref to cl_abap_tabledescr.
  45. field-symbols:<fs_data_s> type any,
  46.                <ft_data_s> type standard table,
  47.                <fs_data_t> type any,
  48.                <ft_data_t> type standard table.


  49. *get type and create data reference

  50. go_struct_type_s ?= cl_abap_typedescr=>DESCRIBE_BY_NAME( p_stbl  ).
  51. go_struct_type_t ?= cl_abap_typedescr=>DESCRIBE_BY_NAME( p_ttbl  ).
  52. go_table_type_s = cl_abap_tabledescr=>CREATE(
  53.     P_LINE_TYPE  = go_struct_type_s
  54. *    P_TABLE_KIND = TABLEKIND_STD
  55. *    P_UNIQUE     = ABAP_FALSE
  56. *    P_KEY        =
  57. *    P_KEY_KIND   = KEYDEFKIND_DEFAULT
  58. ).
  59. *  catch CX_SY_TABLE_CREATION.  " Exception when Creating a Table Type
  60. go_table_type_t = cl_abap_tabledescr=>CREATE(
  61.     P_LINE_TYPE  = go_struct_type_t
  62. *    P_TABLE_KIND = TABLEKIND_STD
  63. *    P_UNIQUE     = ABAP_FALSE
  64. *    P_KEY        =
  65. *    P_KEY_KIND   = KEYDEFKIND_DEFAULT
  66. ).
  67. *  catch CX_SY_TABLE_CREATION.  " Exception when Creating a Table Type

  68. create data gs_data_s type handle go_struct_type_s.
  69. create data gt_data_s type handle go_table_type_s.
  70. create data gs_data_t type handle go_struct_type_t.
  71. create data gt_data_t type handle go_table_type_t.
  72. assign gs_data_s->* to <fs_data_s>.
  73. assign gs_data_t->* to <fs_data_t>.
  74. assign gt_data_s->* to <ft_data_s>.
  75. assign gt_data_t->* to <ft_data_t>.

  76. *fetch data from source table
  77. select * from (p_stbl)
  78.   into table <ft_data_s>.

  79. *copy using move coressponding
  80. loop at <ft_data_s> assigning <fs_data_s>.
  81.   clear <fs_data_t>.
  82.   MOVE-CORRESPONDING <fs_data_s> to <fs_data_t>.
  83.   append <fs_data_t> to <ft_data_t>.
  84. endloop.

  85. *delete data if required
  86. if p_del eq abap_true.
  87.   delete from (p_ttbl).
  88. endif.
  89. *update data
  90. modify (p_ttbl) from table <ft_data_t>.

  91. write:'finish'.
复制代码
回复

使用道具 举报

zhongguomao
学习了。学习了。
回复

使用道具 举报

快速回帖

本版积分规则
您需要登录后才可以回帖 登录 | 注册有礼

快速回复 返回顶部 返回列表