‎2009 May 14 10:22 AM
Hi,
Can anyone tell how to restrict columns while fetching from database.
For ex take the table MARA.
If i enter 3 i should get only 3 columns from MARA table.
I am trying with select query.Is it possible ??
Thanks,
Mohan.
‎2009 May 15 6:31 AM
Hi Mohan,
Find the below code to restrict the columns while querying the DB. The no. of columns can be changed
dynamically. I'm explaining this with VBAK table.
TYPES: BEGIN OF ty_fields,
field TYPE fieldname,
pos TYPE position,
END OF ty_fields.
TYPES: ty_columns TYPE fieldname.
DATA: it_fields TYPE TABLE OF ty_fields,
it_vbak TYPE TABLE OF vbak,
it_columns TYPE TABLE OF ty_columns,
wa_fields TYPE ty_fields,
wa_columns TYPE ty_columns.
CONSTANTS: w_first TYPE tabfdpos VALUE 1.
PARAMETERS: p_column TYPE tabfdpos. "Specify no. of columns
START-OF-SELECTION.
SELECT fieldname
position FROM dd03l
INTO TABLE it_fields
WHERE tabname EQ 'VBAK' AND
position BETWEEN w_first AND p_column.
SORT it_fields BY pos. " Sort by field position (now it is by name)
LOOP AT it_fields INTO wa_fields.
MOVE wa_fields-field TO wa_columns.
APPEND wa_columns TO it_columns.
ENDLOOP.
SELECT (it_columns) FROM vbak INTO TABLE it_vbak.Revert in case of any issue.
Regards,
Manoj Kumar P
‎2009 May 14 10:25 AM
Use upto n rows addition to select
For example select * from mara up to 3 rows into mara where matnr in s_matnr.
Edited by: Gayathri G on May 14, 2009 11:25 AM
‎2009 May 14 10:26 AM
Hello,
I think you can try this from a dynamic select query.
In DD03L, you can get the field name and field position.
With these field position numbers, get the field names and try to build a dynamic select query.
I hope this will work.
Thanks,
Sowmya
‎2009 May 14 10:28 AM
is it column or rows
if columns
select field1 field2 field3
into corresponding fields of table i_tab
from mara.
‎2009 May 14 10:54 AM
Nafran, i dont know the no of columns i will give in runtime.
‎2009 May 14 10:40 AM
Hi ,
You can use dynamic select query...
Hope this code helps.
In case you have doubts.. reply back..
parameters: p_fld1 type fieldname,
p_tab1 type tabname.
types: begin of tp_table,
field type string,
end of tp_table.
data: t_table type standard table of tp_table.
select (p_fld1)
from (p_tab1)
into table t_table
up to 1 rows.
Here the field name and table name are entered in the selection screen
Regards.
‎2009 May 14 10:52 AM
i just have to enter the number and fields should be selected from the table automatically.
‎2009 May 14 10:51 AM
‎2009 May 15 6:31 AM
Hi Mohan,
Find the below code to restrict the columns while querying the DB. The no. of columns can be changed
dynamically. I'm explaining this with VBAK table.
TYPES: BEGIN OF ty_fields,
field TYPE fieldname,
pos TYPE position,
END OF ty_fields.
TYPES: ty_columns TYPE fieldname.
DATA: it_fields TYPE TABLE OF ty_fields,
it_vbak TYPE TABLE OF vbak,
it_columns TYPE TABLE OF ty_columns,
wa_fields TYPE ty_fields,
wa_columns TYPE ty_columns.
CONSTANTS: w_first TYPE tabfdpos VALUE 1.
PARAMETERS: p_column TYPE tabfdpos. "Specify no. of columns
START-OF-SELECTION.
SELECT fieldname
position FROM dd03l
INTO TABLE it_fields
WHERE tabname EQ 'VBAK' AND
position BETWEEN w_first AND p_column.
SORT it_fields BY pos. " Sort by field position (now it is by name)
LOOP AT it_fields INTO wa_fields.
MOVE wa_fields-field TO wa_columns.
APPEND wa_columns TO it_columns.
ENDLOOP.
SELECT (it_columns) FROM vbak INTO TABLE it_vbak.Revert in case of any issue.
Regards,
Manoj Kumar P
‎2009 May 15 6:42 AM