其实很简单就是用move-corresponding语句,匹配两边的相同字段,然后赋值,就可以实现简单地在数据库表之间迁移数据了。
直接上代码
*&---------------------------------------------------------------------*
*& Report ZTABLECOPY
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZTABLECOPY.
parameters:p_stbl type tabname16,"Source table name
p_ttbl type tabname16,"Target table name
p_over as checkbox,"Overwrite existing data
p_del as checkbox."Delete before copy
data:record_count type int4.
data:gs_table type dd02l.
at selection-screen on p_stbl."Check existence of source table
select single *
from dd02l
into gs_table
where tabname = p_stbl
and as4local = 'A'.
if sy-subrc ne 0.
message 'Source table not exists' type 'E'.
endif.
at selection-screen on p_ttbl."Check existence of target table
select single *
from dd02l
into gs_table
where tabname = p_ttbl
and as4local = 'A'.
if sy-subrc ne 0.
message 'Target table not exists' type 'E'.
endif.
select count(*) into record_count from (p_ttbl).
if record_count gt 0 and p_over is initial and p_del is initial.
message 'Target table has data' type 'E'.
endif.
data:gt_data_s type ref to data,
gs_data_s type ref to data,
go_struct_type_s type ref to cl_ABAP_structdescr,
go_table_type_s type ref to cl_abap_tabledescr,
gt_data_t type ref to data,
gs_data_t type ref to data,
go_struct_type_t type ref to cl_abap_structdescr,
go_table_type_t type ref to cl_abap_tabledescr.
field-symbols:<fs_data_s> type any,
<ft_data_s> type standard table,
<fs_data_t> type any,
<ft_data_t> type standard table.
*get type and create data reference
go_struct_type_s ?= cl_abap_typedescr=>DESCRIBE_BY_NAME( p_stbl ).
go_struct_type_t ?= cl_abap_typedescr=>DESCRIBE_BY_NAME( p_ttbl ).
go_table_type_s = cl_abap_tabledescr=>CREATE(
P_LINE_TYPE = go_struct_type_s
* P_TABLE_KIND = TABLEKIND_STD
* P_UNIQUE = ABAP_FALSE
* P_KEY =
* P_KEY_KIND = KEYDEFKIND_DEFAULT
).
* catch CX_SY_TABLE_CREATION. " Exception when Creating a Table Type
go_table_type_t = cl_abap_tabledescr=>CREATE(
P_LINE_TYPE = go_struct_type_t
* P_TABLE_KIND = TABLEKIND_STD
* P_UNIQUE = ABAP_FALSE
* P_KEY =
* P_KEY_KIND = KEYDEFKIND_DEFAULT
).
* catch CX_SY_TABLE_CREATION. " Exception when Creating a Table Type
create data gs_data_s type handle go_struct_type_s.
create data gt_data_s type handle go_table_type_s.
create data gs_data_t type handle go_struct_type_t.
create data gt_data_t type handle go_table_type_t.
assign gs_data_s->* to <fs_data_s>.
assign gs_data_t->* to <fs_data_t>.
assign gt_data_s->* to <ft_data_s>.
assign gt_data_t->* to <ft_data_t>.
*fetch data from source table
select * from (p_stbl)
into table <ft_data_s>.
*copy using move coressponding
loop at <ft_data_s> assigning <fs_data_s>.
clear <fs_data_t>.
MOVE-CORRESPONDING <fs_data_s> to <fs_data_t>.
append <fs_data_t> to <ft_data_t>.
endloop.
*delete data if required
if p_del eq abap_true.
delete from (p_ttbl).
endif.
*update data
modify (p_ttbl) from table <ft_data_t>.
write:'finish'.