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

how can i get data (dynamically field and table name) in a function?

Former Member
0 Likes
6,972

Hi experts,

Actually my first question is, how can i get data with a function. Than i want to get dynamically fields and tables for my query in a function.

I have a database and i want to get data use function. Everything ok. I created a function than i used in program but it is giving me type error.

How did i it?

I created ZP_VERI_GETIR function like below.



function's source code

One more problem : Imported parameter name is 'TABLO' but i have never used it

SELECT * FROM ZPMAK INTO CORRESPONDING FIELDS OF

     TABLE gt_itab.

IF SY-SUBRC EQ 0.

MESSAGE 'VERİLER ALINDI' TYPE 'I'.

ENDIF.

ENDFUNCTION.

I called my function in program.

DATA itab like zpmak occurs 0.

       CALL FUNCTION 'ZP_VERI_GETIR'

         EXPORTING

           tablo   = zpmak

         IMPORTING

           gt_itab = itab.

But i am getting type error. How can i solve it.


1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
4,068

hi, guy

you can write at table tab as parameter, or changing tab.

Hi experts,

Actually my first question is, how can i get data with a function. Than i want to get dynamically fields and tables for my query in a function.

I have a database and i want to get data use function. Everything ok. I created a function than i used in program but it is giving me type error.

How did i it?

I created ZP_VERI_GETIR function like below.



function's source code

One more problem : Imported parameter name is 'TABLO' but i have never used it

SELECT * FROM ZPMAK INTO CORRESPONDING FIELDS OF

     TABLE gt_itab.

IF SY-SUBRC EQ 0.

MESSAGE 'VERİLER ALINDI' TYPE 'I'.

ENDIF.

ENDFUNCTION.

I called my function in program.

DATA itab like zpmak occurs 0.

       CALL FUNCTION 'ZP_VERI_GETIR'

         EXPORTING

           tablo   = zpmak

         IMPORTING

           gt_itab = itab.

But i am getting type error. How can i solve it.


20 REPLIES 20
Read only

Former Member
0 Likes
4,068

Hi Omer,

it's hard to see in the image because of the quality. Please check your import/export parameters and the type again. There must be a difference somewhere otherwise you won't get the type error

Regards,

Ioan.

Read only

0 Likes
4,068

Thanks for your answer.

Pls Click on image than you can see easily

Read only

0 Likes
4,068

Ok, try to declare your variables like this: DATA: itab         type z_table,

                                                                             z_zpmak type z_table.      

CALL FUNCTION 'ZP_VERI_GETIR'

         EXPORTING

           tablo   = z_zpmak

         IMPORTING

           gt_itab = itab.

Read only

0 Likes
4,068

Bingoo! it is working

Also i want to learn how can i get dynamically field and table name with function. Can you tell me how can i do it?

Thanks for your help.

Read only

former_member219162
Contributor
0 Likes
4,068

Hello Omar,

Some changes are required in your Function Module;

1) Import Parameter


TABLO    TYPE      DBOBJ_NAME

2) Export Parameter is Ok

3) In Source Code;

SELECT * FROM (TABLO) INTO CORRESPONDING FIELDS OF

     TABLE gt_itab.

4)

       CALL FUNCTION 'ZP_VERI_GETIR'

         EXPORTING

           tablo   = 'ZPMAK'

         IMPORTING

           gt_itab = itab.

We are trying to Pass the Tablename, "ZPMAK". In the function module we would query the table name dynamically.

Read only

0 Likes
4,068

Thanks for your answer.

I have to use table type. Thats ok.

Next question is How can i get dynamically field and table name with function?

Read only

0 Likes
4,068

Hi Omer,

the dynamic stuff is pretty complicated and I don't understand your question. You have to be more explicit about what you want to know.

Regards,

Ioan.

Read only

Former Member
0 Likes
4,069

hi, guy

you can write at table tab as parameter, or changing tab.

Read only

0 Likes
4,068

Can u tell me on example?

Thanks for your answer.

Read only

0 Likes
4,068

Hi Omer

What exactly do you mean by field and table name..?  Can you please elaborate. I do understdan you pass table name and it fetches the data for the passed table name

Nabheet

Read only

0 Likes
4,068

I want to 2 parameter on selection screen. I can choose table name and field names. When i clicked button i want to see my selected table and field data's on write screen.

I hope u have understood me.

Read only

0 Likes
4,068

hi, guy , I want to show you this screen spot. but I dont konw what is this web site suport pic format?

you can write table parameter name in  tables tab, tying is LIKE, and Associated Type is a structure for table type.  When you actived your function completed, you can user Ctrl + F6 to selected your just finished function in code edit, and then you can find the table parameter auto-displayed.

Read only

0 Likes
4,068

you can do as follow

create two parameters as importing one in Function module

One for table name which you already have and another for field name.

and one for table parameter which will return you data.

In field name you can concatenatte all field names like

concatenate 'BUKRS' 'MANDT' into IM_FIELDNAME (you can have n number of field)

Select (im_fieldname) from (im_tablename) into table IT_TAB.

Read only

0 Likes
4,068

Hi Omer,

Please try this

Parameters:  TABLE_NAME     like DCOBJDEF-NAME.         "Enter the table Name

  DATA: data_refe1 TYPE REF TO data,

             data_refe2 TYPE REF TO data ,

            tab_fldcat TYPE TABLE OF lvc_s_fcat,

            wa_fldcat  LIKE LINE OF tab_fldcat.

  DATA: BEGIN OF table_dtls OCCURS 0.

          INCLUDE STRUCTURE dntab.

  DATA: END OF table_dtls.

  REFRESH table_dtls.

  CALL FUNCTION 'NAMETAB_GET'

    EXPORTING

      langu          = sy-langu

      tabname        = table_name

    TABLES

      nametab        = table_dtls

    EXCEPTIONS

      no_texts_found = 1.

  LOOP AT table_dtls .

    wa_fldcat-fieldname = table_dtls-fieldname.

    wa_fldcat-ref_table = table_name.

    wa_fldcat-ref_field = table_dtls-fieldname.

    APPEND wa_fldcat TO tab_fldcat.

  ENDLOOP.

* Build the internal table

  CALL METHOD cl_alv_table_create=>create_dynamic_table

    EXPORTING

      it_fieldcatalog = tab_fldcat

    IMPORTING

      ep_table        = data_refe1.

  ASSIGN data_refe1->* TO <fs>.

  SELECT * FROM (table_name)

     INTO CORRESPONDING FIELDS OF TABLE <fs>.

Read only

0 Likes
4,068

I want to create my function.

Thanks for  your help.

Read only

0 Likes
4,068

FUNCTION ZP_DINAMIK_DATA.

*"----------------------------------------------------------------------

*"*"Local Interface:

*"  IMPORTING

*"     REFERENCE(TABLE) TYPE  CHAR20

*"  EXPORTING

*"     REFERENCE(GT_ITAB) TYPE  DATA

*"----------------------------------------------------------------------

field-symbols: <f1> type any table.

create data gt_itab type (TABLE).

assign gt_itab->* to <f1>.

SELECT * FROM (TABLE) INTO CORRESPONDING FIELDS OF

     TABLE <f1>.

IF SY-SUBRC EQ 0.

MESSAGE 'VERİLER ALINDI' TYPE 'I'.

ENDIF.

ENDFUNCTION.

PROGRAM'S CODE

*&---------------------------------------------------------------------*

*& Report  ZP_DINAMIK_DATA

*&

*&---------------------------------------------------------------------*

*&

*&

*&---------------------------------------------------------------------*

REPORT  ZP_DINAMIK_DATA.

SELECTION-SCREEN BEGIN OF BLOCK blok1 WITH FRAME TITLE bilgi1.

PARAMETERS p_table TYPE char30.

"PARAMETERS p_field TYPE char30.

SELECTION-SCREEN END OF BLOCK blok1.

SELECTION-SCREEN BEGIN OF LINE.

SELECTION-SCREEN PUSHBUTTON (60) getdy USER-COMMAND btn5.

SELECTION-SCREEN END OF LINE.

INITIALIZATION.

getdy = 'get data'.

start-of-selection.

    DATA: itab  like p_table occurs 0.

CASE SY-UCOMM.

   WHEN 'BTN5'.

     CALL FUNCTION 'ZP_DINAMIK_DATA'

     EXPORTING

       table        = p_table

    IMPORTING

     GT_ITAB       = itab

             .

ENDCASE.

not working and no errors.

Read only

0 Likes
4,068

In debugging does the select inside FM return anything?

Read only

0 Likes
4,068

REPORT  ZP_DINAMIK_DATA.

SELECTION-SCREEN BEGIN OF BLOCK blok1 WITH FRAME TITLE bilgi1.

PARAMETERS p_table TYPE char30.

"PARAMETERS p_field TYPE char30.

SELECTION-SCREEN END OF BLOCK blok1.

SELECTION-SCREEN BEGIN OF LINE.

SELECTION-SCREEN PUSHBUTTON (60) getdy USER-COMMAND btn5.

SELECTION-SCREEN END OF LINE.

INITIALIZATION.

getdy = 'get data'.

at selection-screen.

    DATA: itab  like p_table occurs 0.

CASE SY-UCOMM.

   WHEN 'BTN5'.

     CALL FUNCTION 'ZP_DINAMIK_DATA'

     EXPORTING

       table        = p_table

    IMPORTING

     GT_ITAB       = itab

             .

ENDCASE.

i am getting type error....

Read only

0 Likes
4,068

You can do as follow. Use data ref to in tables parameter.

Program
DATA gr_ref TYPE REF TO DATA
FIELD-SYMBOLS <fs_data> TYPE STANDARD TABLE.
CALL FUNCTION ''ZP_DINAMIK_DATA'
  IMPORTING
IT_DATA = gr_ref
ASSIGN gr_ref->* TO <lt_table>.

In function module ZP_DINAMIK_DATA

DATA lr_ref TYPE REF TO DATA
Create dynamic internal table as told in earlier post by anriban. Then fill the data using select * and proceed.
IT_DATA = lr_ref

Read only

0 Likes
4,068

Thanks for your help.

function module ZP_DINAMIK_DATA

field-symbols: <f1> type  table .

DATA lr_ref TYPE REF TO DATA.

create data IT_DATA type table of (TABLE).

assign IT_DATA->* to <f1>.

SELECT  * FROM (TABLE) INTO CORRESPONDING FIELDS OF table

      <f1>.

IF SY-SUBRC EQ 0.

MESSAGE 'VERİLER ALINDI' TYPE 'I'.

ENDIF.

Program

             CALL FUNCTION 'ZP_DINAMIK_DATA'

               EXPORTING

                 table         = p_table

              IMPORTING

*               GT_ITAB       = itab

                IT_DATA       = gr_ref

                       .

             ASSIGN gr_ref->* TO <lt_table>.

Regards,

Omer.