FUNCTION Z_FS_ASSIGN.
*"--------------------------------------------------------------------
*"*"로컬인터페이스:
*" IMPORTING
*" VALUE(TABNM) TYPE ZOEFAXT_MNGJOB-TABL
*" VALUE(LAYNM) TYPE ZOEFAXT_MNGJOB-LAYO
*" EXPORTING
*" VALUE(GR_DBSTR) TYPE REF TO DATA
*" VALUE(GR_DBTAB) TYPE REF TO DATA
*" VALUE(GR_FLSTR) TYPE REF TO DATA
*" VALUE(ERR_MSG) TYPE STRING
*"--------------------------------------------------------------------
FIELD-SYMBOLS: <DBTAB> TYPE STANDARD TABLE,
<DBSTR> TYPE ANY,
<FLSTR> TYPE ANY.
DATA: RESULT TYPE P DECIMALS 2,
OREF TYPE REF TO CX_ROOT,
TEXT TYPE STRING.
TRY.
*" db table : body
CREATE DATA GR_DBTAB TYPE STANDARD TABLE OF (TABNM).
*" db structure : header
CREATE DATA GR_DBSTR TYPE (TABNM).
*" file structure : if str
CREATE DATA GR_FLSTR TYPE (LAYNM).
CATCH CX_SY_CREATE_DATA_ERROR INTO OREF.
TEXT = OREF->GET_TEXT( ).
ENDTRY.
ERR_MSG = TEXT.
ENDFUNCTION.
동적 인터널 테이블, 스트럭쳐, 필드에 값 넘겨주기...... 만들기,
dynamic internal table, dynamic Structure, dynamic field 생성 및 만들기.
하단 프로그램에 이 세가지 포인트를 다 담고 있다.
DATA : lv_line TYPE int4,
l_generic_struc TYPE REF TO data,
l_generic_table TYPE REF TO data,
lv_index TYPE sy-tabix.
DATA : d_tab TYPE REF TO data,
d_line TYPE REF TO data,
lt_fcat TYPE TABLE OF lvc_s_fcat,
ls_fcat LIKE LINE OF lt_fcat,
nametab LIKE dntab OCCURS 0 WITH HEADER LINE.
DATA : tdd02l LIKE dd02l.
FIELD-SYMBOLS : <table> TYPE ANY, "안씀...
<struc> TYPE ANY,
<field> TYPE ANY.
FIELD-SYMBOLS : <new_tab> TYPE table,
<new_line> TYPE ANY.
PARAMETERS : p_tname LIKE dd03l-tabname OBLIGATORY.
AT SELECTION-SCREEN.
SELECT * FROM dd02l "db가 존재하는지 체크한다.
INTO tdd02l
WHERE tabname = p_tname
AND as4local = 'A'
AND ( tabclass <> 'INTTAB'
AND tabname <> 'APPEND'
AND tabclass <> 'VIEW' ).
EXIT.
ENDSELECT.
IF sy-subrc <> 0. MESSAGE e402(mo) WITH p_tname. ENDIF.
START-OF-SELECTION.
* CREATE DATA l_generic_table TYPE TABLE OF (p_tname).
* ASSIGN l_generic_table->* TO <table>.
*
* CREATE DATA l_generic_struc TYPE (p_tname).
* ASSIGN l_generic_struc->* TO <struc>.
* Field catalog get
CALL FUNCTION 'NAMETAB_GET'
EXPORTING
langu = sy-langu
tabname = p_tname
TABLES
nametab = nametab
EXCEPTIONS
no_texts_found = 1.
SORT nametab BY position.
CLEAR : lt_fcat[].
LOOP AT nametab.
* Internal table
IF sy-subrc = 0.
ls_fcat-fieldname = nametab-fieldname.
ls_fcat-ref_table = p_tname.
ls_fcat-ref_field = nametab-fieldname.
APPEND ls_fcat TO lt_fcat.
ENDIF.
ENDLOOP.
IF lt_fcat[] IS INITIAL.
STOP.
ENDIF.
* Internal table create "dynamic Internal table
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = lt_fcat
IMPORTING
ep_table = d_tab.
ASSIGN d_tab->* TO <new_tab>. "fs에 동적 테이블 선언
CREATE DATA d_line LIKE LINE OF <new_tab>.
ASSIGN d_line->* TO <new_line>. "fs에 동적 스트럭쳐 선언
SELECT * FROM (p_tname) INTO TABLE <new_tab>. "데이터 가져와서 동적 테이블에 담기.
IF sy-subrc = 0.
LOOP AT <new_tab> ASSIGNING <new_line>.
READ TABLE nametab WITH KEY fieldname = 'BUKRS'.
lv_index = nametab-position. "필드 포지션을 읽음...
ASSIGN COMPONENT lv_index OF STRUCTURE <new_line> TO <field>. "필드 내용을 동적으로 담기.
IF <field> = '2000'.
<field> = '1000'.
ELSE.
WRITE : '예외 회사코드가 있습니다'.
STOP.
ENDIF.
ENDLOOP.
INSERT (p_tname) FROM TABLE <new_tab>.
IF sy-subrc = 0.
DESCRIBE TABLE <new_tab> LINES lv_line.
COMMIT WORK.
WRITE :lv_line , '건이 들어갔습니다'.
ELSE.
ROLLBACK WORK.
WRITE : '데이터 저장 실패!!!!!'.
ENDIF.
ENDIF.
END-OF-SELECTION.
단, 모든 프로그램을 동적으로 짜면 나중에 유지보수가 어려워진다는 것을..... 참고합시당.^^
내가 만든 프로그램... 남이 수정할수도 있으니...
'ERP-SAP > ABAP' 카테고리의 다른 글
<ABAP> DB수정 프로그램 (0) | 2013.11.06 |
---|---|
<ABAP> Data Element read 하기, Table 정보 등.'DDIF_FIELDINFO_GET' 'NAMETAB_GET' (0) | 2013.09.11 |
<ABAP> BATCH JOB LIST , 배치 잡 전체 리스트 확인 (0) | 2013.08.27 |
<ABAP> Mainternence view event 01 생성/수정시간넣기 (0) | 2013.08.26 |
<ABAP> WRITE ~ TO ~ USING EDIT MASK 활용 (0) | 2013.07.22 |