‎2007 Oct 01 12:11 PM
Hi,
I need to have a Screen that has an input field for the user to enter the table name. The code behind will need to be able to know the fieldname (column) of the table schema. It also must do a simple validation on all the data. It will return the row number and the fieldname (column) of the data if it is blank. This is a dynamic program, regardless of SAP table 'spfli' or custom table that user define. It should work well also. Is it possible to do that? How to do and what is the code like?
Best Regards,
Rayden
‎2007 Oct 01 12:29 PM
Hi,
Check the code below:
REPORT ZYKTEST3 .
DATA: d_ref TYPE REF TO data,
d_ref2 TYPE REF TO data,
i_alv_cat TYPE TABLE OF lvc_s_fcat,
ls_alv_cat LIKE LINE OF i_alv_cat.
TYPES: tabname LIKE dcobjdef-name ,
fieldname LIKE dcobjdef-name,
desc LIKE dntab-fieldtext.
PARAMETER: p_tablen TYPE tabname. -
> <b>Input table field</b>
DATA: BEGIN OF itab OCCURS 0.
INCLUDE STRUCTURE dntab.
DATA: END OF itab.
FIELD-SYMBOLS : <f_fs> TYPE table,
<f_fs1> TYPE table,
<f_fs2> TYPE ANY,
<f_fs3> TYPE ANY,
<f_fs4> type any,
<f_field> TYPE ANY.
REFRESH itab.
CALL FUNCTION 'NAMETAB_GET' -
> <b>Fetches the fields</b>
EXPORTING
langu = sy-langu
tabname = p_tablen
TABLES
nametab = itab
EXCEPTIONS
no_texts_found = 1.
LOOP AT itab .
ls_alv_cat-fieldname = itab-fieldname.
ls_alv_cat-ref_table = p_tablen.
ls_alv_cat-ref_field = itab-fieldname.
ls_alv_cat-seltext = itab-fieldtext.
ls_alv_cat-reptext = itab-fieldtext.
APPEND ls_alv_cat TO i_alv_cat.
ENDLOOP.
internal table build
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = i_alv_cat
IMPORTING
ep_table = d_ref.
ASSIGN d_ref->* TO <f_fs>. -
> <b>Dynamic table creation with fields of the table</b>
DATA: l_field TYPE fieldname,
l_field1 type fieldname.
<b>SELECT * FROM (p_tablen) INTO CORRESPONDING FIELDS OF TABLE <f_fs>.
Fetching of the data from the table</b>
LOOP AT <f_fs> ASSIGNING <f_fs2>.
<b>Here u can check the validations and process</b>
ASSIGN COMPONENT 2 OF STRUCTURE <f_fs2> TO <f_fs3>.
ASSIGN COMPONENT 3 OF STRUCTURE <f_fs2> TO <f_fs4>.
IF sy-subrc = 0.
MOVE <f_fs3> TO l_field.
MOVE <f_fs4> TO l_field1.
WRITE:/1 l_field(20),
22 l_field1(10).
ENDIF.
ENDLOOP.
Regards
Kannaiah
‎2007 Oct 01 12:27 PM
Rayden,
Use the FM <b>DB_GET_TABLE_FIELDS</b> for this purpose.
Pass the table name in <b>TABNAME</b> parameter and it will return a table (thru the TABLES parameter) which u can capture in an internal table with structure <b>DBFIELD</b>.
This should solve ur query,
Rerward if helpful
Karthik
‎2007 Oct 01 12:29 PM
Hi,
Check the code below:
REPORT ZYKTEST3 .
DATA: d_ref TYPE REF TO data,
d_ref2 TYPE REF TO data,
i_alv_cat TYPE TABLE OF lvc_s_fcat,
ls_alv_cat LIKE LINE OF i_alv_cat.
TYPES: tabname LIKE dcobjdef-name ,
fieldname LIKE dcobjdef-name,
desc LIKE dntab-fieldtext.
PARAMETER: p_tablen TYPE tabname. -
> <b>Input table field</b>
DATA: BEGIN OF itab OCCURS 0.
INCLUDE STRUCTURE dntab.
DATA: END OF itab.
FIELD-SYMBOLS : <f_fs> TYPE table,
<f_fs1> TYPE table,
<f_fs2> TYPE ANY,
<f_fs3> TYPE ANY,
<f_fs4> type any,
<f_field> TYPE ANY.
REFRESH itab.
CALL FUNCTION 'NAMETAB_GET' -
> <b>Fetches the fields</b>
EXPORTING
langu = sy-langu
tabname = p_tablen
TABLES
nametab = itab
EXCEPTIONS
no_texts_found = 1.
LOOP AT itab .
ls_alv_cat-fieldname = itab-fieldname.
ls_alv_cat-ref_table = p_tablen.
ls_alv_cat-ref_field = itab-fieldname.
ls_alv_cat-seltext = itab-fieldtext.
ls_alv_cat-reptext = itab-fieldtext.
APPEND ls_alv_cat TO i_alv_cat.
ENDLOOP.
internal table build
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = i_alv_cat
IMPORTING
ep_table = d_ref.
ASSIGN d_ref->* TO <f_fs>. -
> <b>Dynamic table creation with fields of the table</b>
DATA: l_field TYPE fieldname,
l_field1 type fieldname.
<b>SELECT * FROM (p_tablen) INTO CORRESPONDING FIELDS OF TABLE <f_fs>.
Fetching of the data from the table</b>
LOOP AT <f_fs> ASSIGNING <f_fs2>.
<b>Here u can check the validations and process</b>
ASSIGN COMPONENT 2 OF STRUCTURE <f_fs2> TO <f_fs3>.
ASSIGN COMPONENT 3 OF STRUCTURE <f_fs2> TO <f_fs4>.
IF sy-subrc = 0.
MOVE <f_fs3> TO l_field.
MOVE <f_fs4> TO l_field1.
WRITE:/1 l_field(20),
22 l_field1(10).
ENDIF.
ENDLOOP.
Regards
Kannaiah
‎2007 Oct 02 8:41 AM
Hi Kannaiah,
That was very useful. Is there any possible way to create a table from <f_fs> into a real table in sap.
I was thinking, to get table A into <f_fs> then from there do validation and modifying the value and create another table B out of it. Any available function or method can do that?
Regards
Rayden
‎2007 Oct 02 8:54 AM
Hi,
I dont think it would be possible to create a table from <f_fs> into a real table in SAP. Let me also try from my end whether it would be possible or not.
Regards
Kannaiah
‎2007 Oct 02 10:27 AM
Hi Kannaiah,
I have modify the code given to do a simple validation to validate for NULL value in all data regardless of the field. It will return the Row number and the Column number of the error data. How do i get the field name of that particular column?
Here the sample code.
===========================================================
REPORT ZTEST_LOOP2.
DATA: d_ref TYPE REF TO data,
i_alv_cat TYPE TABLE OF lvc_s_fcat,
ls_alv_cat LIKE LINE OF i_alv_cat,
fname TYPE fieldname,
ncomp(9) TYPE n,
rcount(9) TYPE n,
fcount(3) TYPE n.
ncomp = 1.
rcount = 0.
TYPES: tabname LIKE dcobjdef-name,
fieldname LIKE dcobjdef-name.
PARAMETER: p_tablen TYPE tabname. "Input table field
DATA: BEGIN OF itab OCCURS 0.
INCLUDE STRUCTURE dntab.
DATA: END OF itab.
FIELD-SYMBOLS : <f_fs> TYPE TABLE,
<f_fs2> TYPE ANY,
<f_fs3> TYPE ANY,
<f_fs4> TYPE ANY.
REFRESH itab.
CALL FUNCTION 'NAMETAB_GET' "Fetches the fields
EXPORTING
langu = sy-langu
tabname = p_tablen
TABLES
nametab = itab
EXCEPTIONS
no_texts_found = 1.
LOOP AT itab .
ls_alv_cat-fieldname = itab-fieldname.
ls_alv_cat-ref_table = p_tablen.
ls_alv_cat-ref_field = itab-fieldname.
ls_alv_cat-seltext = itab-fieldtext.
ls_alv_cat-reptext = itab-fieldtext.
APPEND ls_alv_cat TO i_alv_cat.
fcount = fcount + 1.
ENDLOOP.
WRITE:/ fcount.
WRITE: ' number of column'.
internal table build
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = i_alv_cat
IMPORTING
ep_table = d_ref.
ASSIGN d_ref->* TO <f_fs>. " Dynamic table creation with fields of the table
DATA: l_field TYPE fieldname,
l_field1 type fieldname.
SELECT * FROM (p_tablen) INTO CORRESPONDING FIELDS OF TABLE <f_fs>.
"Fetching of the data from the table
do fcount times.
rcount = 0.
LOOP AT <f_fs> ASSIGNING <f_fs2>.
rcount = rcount + 1.
"Here u can check the validations and process
ASSIGN COMPONENT ncomp OF STRUCTURE <f_fs2> TO <f_fs4>.
IF <f_fs4> IS INITIAL.
MOVE <f_fs4> TO l_field1.
WRITE:/ 'Row '.
WRITE: rcount.
WRITE: 'in column'.
WRITE: ncomp.
WRITE: 'is blank'.
ENDIF.
ENDLOOP.
ncomp = ncomp + 1.
ENDdo.
WRITE:/ rcount.
WRITE: ' number of rows'.
===========================================================
Regards,
Rayden
‎2007 Oct 02 1:17 PM
Hi,
I have done the modifications, check the code:
DATA: d_ref TYPE REF TO data,
i_alv_cat TYPE TABLE OF lvc_s_fcat,
ls_alv_cat LIKE LINE OF i_alv_cat,
fname TYPE fieldname,
ncomp(9) TYPE n,
rcount(9) TYPE n,
fcount(3) TYPE n.
TYPES: BEGIN OF itab1,
message(50) TYPE c,
END OF itab1.
DATA: g_itab1 TYPE STANDARD TABLE OF itab1.
DATA: wa_itab1 TYPE itab1.
ncomp = 1.
rcount = 0.
TYPES: tabname LIKE dcobjdef-name,
fieldname LIKE dcobjdef-name.
PARAMETER: p_tablen TYPE tabname. "Input table field
DATA: BEGIN OF itab OCCURS 0.
INCLUDE STRUCTURE dntab.
DATA: END OF itab.
FIELD-SYMBOLS : <f_fs> TYPE table,
<f_fs2> TYPE ANY,
<f_fs3> TYPE ANY,
<f_fs4> TYPE ANY.
REFRESH itab.
CALL FUNCTION 'NAMETAB_GET' "Fetches the fields
EXPORTING
langu = sy-langu
tabname = p_tablen
TABLES
nametab = itab
EXCEPTIONS
no_texts_found = 1.
LOOP AT itab .
ls_alv_cat-fieldname = itab-fieldname.
ls_alv_cat-ref_table = p_tablen.
ls_alv_cat-ref_field = itab-fieldname.
ls_alv_cat-seltext = itab-fieldtext.
ls_alv_cat-reptext = itab-fieldtext.
APPEND ls_alv_cat TO i_alv_cat.
fcount = fcount + 1.
ENDLOOP.
WRITE:/ fcount.
WRITE: ' number of column'.
internal table build
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = i_alv_cat
IMPORTING
ep_table = d_ref.
ASSIGN d_ref->* TO <f_fs>. " Dynamic table creation with fields of the
*table
DATA: l_field TYPE fieldname,
l_field1 TYPE fieldname.
SELECT * FROM (p_tablen) INTO CORRESPONDING FIELDS OF TABLE <f_fs>.
"Fetching of the data from the table
rcount = 0.
LOOP AT <f_fs> ASSIGNING <f_fs2>.
rcount = rcount + 1.
DO fcount TIMES.
"Here u can check the validations and process
ASSIGN COMPONENT ncomp OF STRUCTURE <f_fs2> TO <f_fs4>.
IF NOT <f_fs4> IS INITIAL.
MOVE <f_fs4> TO l_field1.
ncomp = ncomp + 1.
ELSE.
CONCATENATE 'IN ROW' rcount 'COLUMN' ncomp 'IS BLANK' INTO wa_itab1.
APPEND wa_itab1 TO g_itab1.
ncomp = ncomp + 1.
ENDIF.
ENDDO.
CLEAR:ncomp.
ENDLOOP.
LOOP AT g_itab1 INTO wa_itab1.
WRITE:/ wa_itab1 'is blank'.
ENDLOOP.
Regards
Kannaiah
Message was edited by:
Kannaiah Kavuri
‎2007 Oct 03 6:49 AM
Hi
I have try your code, but it nver get the field name. I have the code below, g_itab1 actually store all the fieldname of the particular table. How do i get the fieldname out of g_itab1 when that particular column have a NULL value? I need the code to be coded in the @TODO@ tag.
Sample code here:
===========================================================
REPORT ZTEST_LOOP2.
DATA: d_ref TYPE REF TO data,
i_alv_cat TYPE TABLE OF lvc_s_fcat,
ls_alv_cat LIKE LINE OF i_alv_cat,
ncomp(9) TYPE n,
rcount(9) TYPE n,
fcount(3) TYPE n.
ncomp = 1.
rcount = 0.
TYPES: tabname LIKE dcobjdef-name,
fieldname LIKE dcobjdef-name.
PARAMETER: p_tablen TYPE tabname. "Input table field
DATA: BEGIN OF itab OCCURS 0.
INCLUDE STRUCTURE dntab.
DATA: END OF itab.
TYPES: BEGIN OF itab1,
fname(50) TYPE c,
END OF itab1.
DATA: g_itab1 TYPE STANDARD TABLE OF itab1.
DATA: wa_itab1 TYPE itab1.
FIELD-SYMBOLS : <f_fs> TYPE TABLE,
<f_fs2> TYPE ANY,
<f_fs3> TYPE ANY,
<f_fs4> TYPE ANY.
REFRESH itab.
CALL FUNCTION 'NAMETAB_GET' "Fetches the fields
EXPORTING
langu = sy-langu
tabname = p_tablen
TABLES
nametab = itab
EXCEPTIONS
no_texts_found = 1.
LOOP AT itab .
ls_alv_cat-fieldname = itab-fieldname.
ls_alv_cat-ref_table = p_tablen.
ls_alv_cat-ref_field = itab-fieldname.
ls_alv_cat-seltext = itab-fieldtext.
ls_alv_cat-reptext = itab-fieldtext.
wa_itab1-fname = itab-fieldname.
APPEND ls_alv_cat TO i_alv_cat.
APPEND wa_itab1 TO g_itab1.
fcount = fcount + 1.
ENDLOOP.
WRITE:/ fcount.
WRITE: ' number of column'.
internal table build
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = i_alv_cat
IMPORTING
ep_table = d_ref.
ASSIGN d_ref->* TO <f_fs>. " Dynamic table creation with fields of the table
DATA: l_field TYPE fieldname,
l_field1 type fieldname.
SELECT * FROM (p_tablen) INTO CORRESPONDING FIELDS OF TABLE <f_fs>.
"Fetching of the data from the table
do fcount times.
rcount = 0.
LOOP AT <f_fs> ASSIGNING <f_fs2>.
rcount = rcount + 1.
"Here u can check the validations and process
ASSIGN COMPONENT ncomp OF STRUCTURE <f_fs2> TO <f_fs4>.
IF <f_fs4> IS INITIAL.
MOVE <f_fs4> TO l_field1.
WRITE:/ 'Row '.
WRITE: rcount.
WRITE: 'in column'.
WRITE: ncomp.
WRITE: 'is blank'.
*@TODO@ <Need to get the fieldname from g_tab1-fname>
ENDIF.
ENDLOOP.
ncomp = ncomp + 1.
ENDdo.
WRITE:/ rcount.
WRITE: ' number of rows'.
===========================================================
Regards,
Rayden
‎2007 Oct 03 8:21 AM
Hi,
Use the code in bold:
LOOP AT <f_fs> ASSIGNING <f_fs2>.
rcount = rcount + 1.
DO fcount TIMES.
"Here u can check the validations and process
ASSIGN COMPONENT ncomp OF STRUCTURE <f_fs2> TO <f_fs4>.
IF NOT <f_fs4> IS INITIAL.
ncomp = ncomp + 1.
ELSE.
<b> READ TABLE itab INDEX ncomp.
IF sy-subrc EQ 0.
l_field1 = itab-fieldname.
ENDIF.</b>
CONCATENATE 'IN ROW' rcount 'COLUMN' ncomp l_field1 'IS BLANK' INTO
wa_itab1.
APPEND wa_itab1 TO g_itab1.
ncomp = ncomp + 1.
ENDIF.
ENDDO.
CLEAR:ncomp.
ENDLOOP.
LOOP AT g_itab1 INTO wa_itab1.
WRITE:/ wa_itab1.
ENDLOOP.
Regards
Kannaiah
‎2007 Oct 03 10:15 AM