Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

select query for column

Former Member
0 Likes
2,263

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.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,611

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

9 REPLIES 9
Read only

Former Member
0 Likes
1,611

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

Read only

Former Member
0 Likes
1,611

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

Read only

Former Member
0 Likes
1,611

is it column or rows

if columns

select field1 field2 field3 
into corresponding fields of table i_tab
from mara.

Read only

0 Likes
1,611

Nafran, i dont know the no of columns i will give in runtime.

Read only

Former Member
0 Likes
1,611

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.

Read only

0 Likes
1,611

i just have to enter the number and fields should be selected from the table automatically.

Read only

Former Member
0 Likes
1,611

HI,

Search the SCN before posting the basic question

Read only

Former Member
0 Likes
1,612

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

Read only

0 Likes
1,611

Manoj thanks for your timely reply...