This is used in a Scenario where to insert new /update existing record in the Database Tables, if an existing record has a change then it should be updated based on Key or else if it is a new record, insert action should be performed dynamically irrespective of target table/field that is going to be modified.
Updating table record by record is a tedious process, so came up with a solution i.e. using custom program we can update any DB table irrespective of Standard table or custom Table.
Following approach allow you to create a generic program in updating any database table.
Here, we are going to update the DB table using CSV File as input with the data that needs to be inserted or Updated.
Note: No deletion operation is utilized for safety purpose.
Steps to be followed:
1.Data declarations.
2. Create Dynamic Table and Work area to get dynamically any input table and update any target table structure.
"Dynamic Internal Table Defination
CREATE DATA lt_tble TYPE STANDARD TABLE OF (lp_tble).
ASSIGN lt_tble->* TO <lfs_table> .
Here lp_tble is target DB table structure.
"Dynamic Work Area Defination
CREATE DATA lt_line LIKE LINE OF <lfs_table>.
ASSIGN lt_line->* TO <lfs_watable> .
3. Create selection screen design to take path of file from CSV that is going to be updated in DB table and pass the DB table/Target table name.
4.Read File Path from you local system, use FM "KD_GET_FILENAME_ON_F4" to select the path using F4.
5. Use FM GUI_UPLOAD to FM to read file content from path and fill in internal table.
ALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = give the file path from local system, file should be in CSV format with headers in same sequence as of target table.
TABLES
data_tab = lt_itab(internal table that gets updated from file)
EXCEPTIONS
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
OTHERS = 17.
6.Assign Data to Internal Table via Work Area(Here tricky part is to split the Work area that is taken from CSV comma delimited record to internal table that has structure.).
7. Modify Database table with Internal table, If record already exist it will be update else new entry will get created.
8.Save and Activate your code.
9.Execute Program , provide CSV file with Header and provide the Target Table that needs to be updated
Dynamic Internal tables concept comes into existence when the developer is not sure of the structure of the internal table. This concept creates the structure and internal table during run time.
By using this method we can insert/update any table irrespective of the no. of fields/structure.
*** For client specific tables value in column MANDT will automatically gets populated so no need to create a separate column in flat file for the same .
This program only allows you to add new records or update ( based on key ) existing records in a table .Delete functionality is purposefully opted out, as this can accidentally affect important tables .However if someone want to add delete functionality in program, I would suggest to put some authorization constraint as who or which table can use the program to delete data .A custom table can be created to store authorized users or tables that can use deletion functionality and before deletion this table will be referred by program .
Limitations:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 51 | |
| 37 | |
| 22 | |
| 21 | |
| 18 | |
| 10 | |
| 9 | |
| 8 | |
| 7 | |
| 7 |