Posted: Tue Oct 30, 2007 11:05 am Post subject: Dynamic Table Maintenance
Author: shurasap
Compatibility: Version 6.20 and up,(4.6C - some changes required)
Summary: Finaly in version 6.20 we are able to dynamicly create internal table with the structure of database table. This allows us to dynamicly maintain any table. Please use responsibly and restrict to Z-tables only. Functionality enchanced to include dynamic selection screen option
Code:
REPORT zmtable LINE-SIZE 255
LINE-COUNT 65.
*----------------------------------------------------------------------*
* Program written by Stan Shuralyov
* Maintain Table dynamicly (Table name is entered on Selection Screen)
* Very powerfull program. Please maintain authorisation access and
* restrict maintenance to Z-tables
* Version 6.20 and up
* (can be used in 4.6C with some modifications to block try - endtry
* and classes)
*----------------------------------------------------------------------*
* Version was revised thanks to Franзois Henrotte comment
* (www.eponasolutions.com)
* Enhanced functionality with dynamic selection screen
*----------------------------------------------------------------------*
TABLES: sscrfields. "Fields on selection screens
TYPE-POOLS rsds.
DATA ds_clauses TYPE rsds_where.
DATA: BEGIN OF ifield OCCURS 0,
fieldname LIKE dd03l-fieldname,
position LIKE dd03l-position,
keyflag LIKE dd03l-keyflag,
datatype LIKE dd03l-datatype.
DATA: END OF ifield.
DATA: sl_step LIKE sy-tabix,
ss_step LIKE sy-subrc,
ss_act(1) TYPE c,
sl_lines LIKE sy-tfill,
sl_status LIKE sy-subrc,
sl_subrc LIKE sy-subrc,
sl_update(1) TYPE c,
sl_mandt(1) TYPE c,
len(6) TYPE n,
f_value(255) TYPE c,
sl_datum LIKE sy-datum,
sl_uzeit LIKE sy-uzeit,
price1(15) TYPE c,
price2(15) TYPE c,
mess(60) TYPE c,
d_stat LIKE sy-subrc,
m_stat LIKE sy-subrc,
slchar(6) TYPE c.
DATA: ref_ptr TYPE REF TO cx_root. "Root class more common
DATA: text TYPE string.
DATA: sl_index LIKE sy-tabix.
DATA: zauth LIKE dd02l-tabname.
DATA: num TYPE i,
max_len TYPE i,
check_len TYPE i,
sl_sel(1) TYPE c.
SELECTION-SCREEN BEGIN OF LINE.
* text-012 - 'Table Name'
SELECTION-SCREEN COMMENT 1(25) text-012.
PARAMETERS: tabname LIKE dd02l-tabname DEFAULT 'ZSCARE'.
* text-003 - 'Selection'
SELECTION-SCREEN PUSHBUTTON 75(9) text-003 USER-COMMAND sta1.
SELECTION-SCREEN END OF LINE.
* numrows(text) - 'Max Number of ROWS'
PARAMETERS: numrows LIKE sy-subrc DEFAULT '100'.
************************************************************************
* At Selection-Screen *
************************************************************************
AT SELECTION-SCREEN.
CASE sscrfields-ucomm.
WHEN 'STA1'.
CLEAR sl_sel.
CALL FUNCTION 'ZSTAN_SELECTIONS'
EXPORTING
tabname = tabname
IMPORTING
ds_clauses = ds_clauses
EXCEPTIONS
table_not_valid = 1
other_error = 2
OTHERS = 3.
IF sy-subrc = 0.
sl_sel = 'X'.
ENDIF.
ENDCASE.
************************************************************************
*At Selection-Screen Output *
************************************************************************
AT SELECTION-SCREEN OUTPUT.
SELECT SINGLE tabname
INTO tabname
FROM dd02l
WHERE tabname = tabname
AND as4local = 'A'
AND ( tabclass = 'TRANSP' OR tabclass = 'POOL'
OR tabclass = 'CLUSTER' ).
IF sy-subrc <> 0.
MESSAGE 'Table is not valid' TYPE 'S'.
RETURN.
ENDIF.
select single statu
into sl_update
from (zauth)
where tabname = tabname
and bname = sy-uname.
if sy-subrc <> 0.
message 'You are not authorized to view this table' type 'S'.
return.
endif.
END-OF-DEFINITION.
SELECT SINGLE tabname
INTO tabname
FROM dd02l
WHERE tabname = tabname
AND as4local = 'A'
AND ( tabclass = 'TRANSP' OR tabclass = 'POOL'
OR tabclass = 'CLUSTER' ).
IF sy-subrc <> 0.
MESSAGE 'Table is not valid' TYPE 'S'.
RETURN.
ENDIF.
DATA: ptr_itab TYPE REF TO data.
FIELD-SYMBOLS: <fs_itab> TYPE STANDARD TABLE. "ANY TABLE.
CREATE DATA ptr_itab TYPE STANDARD TABLE OF (tabname).
ASSIGN ptr_itab->* TO <fs_itab>.
* For DELETION
DATA: ptr_itd TYPE REF TO data.
FIELD-SYMBOLS: <fs_itd> TYPE STANDARD TABLE.
CREATE DATA ptr_itd TYPE STANDARD TABLE OF (tabname).
ASSIGN ptr_itd->* TO <fs_itd>.
* For MODIFICATION
DATA: ptr_itm TYPE REF TO data.
FIELD-SYMBOLS: <fs_itm> TYPE STANDARD TABLE.
CREATE DATA ptr_itm TYPE STANDARD TABLE OF (tabname).
ASSIGN ptr_itm->* TO <fs_itm>.
DATA: ptr_wtab TYPE REF TO data.
FIELD-SYMBOLS: <fs_wtab> TYPE ANY.
CREATE DATA ptr_wtab TYPE (tabname).
ASSIGN ptr_wtab->* TO <fs_wtab>.
DATA: itabname(15) TYPE c.
itabname = '<fs_wtab>'.
* Standard list status with 'SAVE' button
SET PF-STATUS 'STLI'.
CLEAR sl_update.
* Maintain authorisation access in table ZTABAUTH
* Key fields: tabname - Table name
* bname = sy-uname - User name
* statu = 'X' - maintain
* ' ' - view
* Check authorisation access
acheck.
SELECT fieldname position keyflag datatype
INTO TABLE ifield
FROM dd03l
WHERE tabname = tabname
AND fieldname NOT LIKE '.INCLU%'
ORDER BY position.
FIELD-SYMBOLS: <f1> TYPE ANY.
DATA: tab_field(60) TYPE c,
sline LIKE sy-lisel.
DATA: field_attr LIKE dfies.
DATA: BEGIN OF tfield_attr OCCURS 0.
INCLUDE STRUCTURE field_attr.
DATA: END OF tfield_attr.
LOOP AT ifield.
CALL FUNCTION 'G_FIELD_READ'
EXPORTING
table = tabname
fieldname = ifield-fieldname
text_flag = 'X'
IMPORTING
field_attr = field_attr.
IF sl_sel = 'X'.
SELECT *
FROM (tabname)
INTO TABLE <fs_itab> UP TO numrows ROWS
WHERE (ds_clauses-where_tab).
ELSE.
SELECT *
FROM (tabname)
INTO TABLE <fs_itab> UP TO numrows ROWS.
ENDIF.
DESCRIBE TABLE <fs_itab> LINES sl_lines.
* Show two extra lines to allow addition up to 2 new lines
IF sl_update = 'X'.
DO 2 TIMES.
APPEND INITIAL LINE TO <fs_itab>.
ENDDO.
ENDIF.
DATA: info(22) VALUE 'D - Delete, M - Modify'.
WRITE: / icon_information AS ICON QUICKINFO info.
WRITE ' '.
CLEAR check_len.
LOOP AT tfield_attr.
IF tfield_attr-datatype = 'CLNT'.
CONTINUE.
ENDIF.
len = tfield_attr-outputlen.
IF tfield_attr-keyflag = 'X'.
check_len = check_len + len + 1.
ENDIF.
IF tfield_attr-scrtext_m IS NOT INITIAL.
WRITE: AT (len) tfield_attr-scrtext_m COLOR 1.
ELSE.
WRITE: AT (len) tfield_attr-fieldtext COLOR 1.
ENDIF.
ENDLOOP.
CLEAR ss_step.
CLEAR ss_act.
LOOP AT <fs_itab> INTO <fs_wtab>.
IF sy-tabix LE sl_lines.
ss_step = 1.
ELSE.
CLEAR ss_step.
ENDIF.
* In field SS_STEP put D - to delete record
* M - to modify/add new record
IF sl_update = 'X'.
WRITE:/ icon_change AS ICON.
IF ss_step = 1.
WRITE: ss_act INPUT ON.
ELSE.
ss_act = 'M'.
WRITE: ss_act.
CLEAR ss_act.
ENDIF.
ELSE.
WRITE:/ icon_display AS ICON.
WRITE: ss_act COLOR 2.
ENDIF.
LOOP AT ifield.
* Maintain client dependant tables in the same client
IF ifield-datatype = 'CLNT'.
sl_mandt = 'X'.
CONTINUE.
ENDIF.
CONCATENATE itabname '-' ifield-fieldname INTO tab_field.
ASSIGN (tab_field) TO <f1>.
IF sl_update = 'X'.
IF ifield-keyflag = 'X' AND ss_step IS NOT INITIAL.
WRITE: <f1> COLOR 4.
ELSE.
WRITE: <f1> INPUT ON.
ENDIF.
ELSE.
IF ifield-keyflag = 'X' AND ss_step IS NOT INITIAL.
WRITE: <f1> COLOR 4.
ELSE.
WRITE: <f1> COLOR 2.
ENDIF.
ENDIF.
UNASSIGN <f1>.
ENDLOOP.
ENDLOOP.
IF sy-subrc = 0.
<f1> = sl_uzeit.
ELSE.
text = 'Invalid Time'.
sl_status = 1.
EXIT.
ENDIF.
ENDIF.
ELSEIF tfield_attr-inttype = 'C'.
TRANSLATE f_value TO UPPER CASE.
<f1> = f_value.
ELSE.
TRANSLATE f_value USING ', '.
CONDENSE f_value NO-GAPS.
TRY.
<f1> = f_value.
CATCH cx_root INTO ref_ptr.
text = ref_ptr->get_text( ).
sl_status = 1.
EXIT.
ENDTRY.
ENDIF.
SHIFT sline BY tfield_attr-outputlen PLACES.
SHIFT sline LEFT.
ENDIF.
UNASSIGN <f1>.
ENDLOOP.
IF sl_status = 0.
CASE ss_step.
WHEN 1. "Delete
ADD 1 TO d_stat.
APPEND <fs_wtab> TO <fs_itd>.
WHEN 2. "Modify
ADD 1 TO m_stat.
APPEND <fs_wtab> TO <fs_itm>.
ENDCASE.
ENDIF.
ENDIF.
ENDDO.
IF sl_status = 0.
IF d_stat IS NOT INITIAL.
slchar = d_stat.
CONCATENATE 'Deleted -' slchar 'record.' INTO text
SEPARATED BY space.
DELETE (tabname) FROM TABLE <fs_itd>.
ENDIF.
IF m_stat IS NOT INITIAL.
slchar = m_stat.
CONCATENATE text 'Modified -' slchar 'record.' INTO text
SEPARATED BY space.
MODIFY (tabname) FROM TABLE <fs_itm>.
ENDIF.
IF d_stat IS INITIAL AND m_stat IS INITIAL.
MESSAGE 'No changes were done' TYPE 'S'.
ELSE.
MESSAGE text TYPE 'S'.
ENDIF.
LEAVE.
ELSE.
MESSAGE text TYPE 'I'.
EXIT.
ENDIF.
ELSE.
LEAVE.
ENDIF.
ENDCASE.
*---------------END---------------
* Below is Function for a Dynamic Selection Screen
FUNCTION zstan_selections.
*"----------------------------------------------------------------------
*"*"Local interface:
*" IMPORTING
*" VALUE(TABNAME) LIKE DD02L-TABNAME DEFAULT 'ZSCARE'
*" EXPORTING
*" VALUE(DS_CLAUSES) TYPE RSDS_WHERE
*" EXCEPTIONS
*" TABLE_NOT_VALID
*" OTHER_ERROR
*"----------------------------------------------------------------------
DATA texpr TYPE rsds_texpr.
DATA twhere TYPE rsds_twhere.
DATA trange TYPE rsds_trange.
DATA BEGIN OF qcat. "Selections View for
INCLUDE STRUCTURE rsdsqcat. "Free Selectoptions
DATA END OF qcat.
DATA BEGIN OF tabs OCCURS 10.
INCLUDE STRUCTURE rsdstabs.
DATA END OF tabs.
DATA BEGIN OF fields OCCURS 10.
INCLUDE STRUCTURE rsdsfields.
DATA END OF fields.
DATA BEGIN OF efields OCCURS 10.
INCLUDE STRUCTURE rsdsfields.
DATA END OF efields.
DATA selid LIKE rsdynsel-selid.
DATA actnum LIKE sy-tfill.
DATA title LIKE sy-title VALUE 'Selection Screen'.
DATA: position LIKE dd03l-position.
DATA: keyflag LIKE dd03l-keyflag.
CLEAR fields.
fields-tablename = tabname.
fields-sign = 'I'.
DATA: step LIKE sy-subrc.
SELECT fieldname keyflag position
INTO (fields-fieldname, keyflag, position)
FROM dd03l
WHERE tabname = tabname
AND fieldname NOT LIKE '.INCLU%'
AND datatype NE 'CLNT'
ORDER BY position.
ADD 1 TO step.
CHECK step LE maxnum.
IF keyflag <> 'X'.
efields = fields.
APPEND efields.
ENDIF.
APPEND fields.
ENDSELECT.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG. SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG. Every effort is made to ensure content integrity. Use information on this site at your own risk.