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

Issue with ABAP statement to read data from table

tom_darsy
Participant
0 Likes
3,644

Hi

I am reading table CDHDR in ECC. I am reading the field object value where i need to pass the document number.

The object value field in CDHDR contains value like 001400090000061692014

The document number is 9000006169.

I have to read the table passing document number. I used the below abap statement

read field 1 from table cdhdr where object value like document number.

THis is not working. Please can you suggest what statement to use.

Thanks

12 REPLIES 12
Read only

former_member182470
Active Contributor
0 Likes
2,445

Your ABAP statement looks like general english which ABAP cannot understand.

Try this :

READ Field1 FROM CDHDR

WHERE DOC NUM EQ 9000006169

Read only

Former Member
0 Likes
2,445

Hi,

Use this below code.

TARGET-FIELD =  CDHDR-FIELD+7(10).

Thanks,

Anji

Read only

tom_darsy
Participant
0 Likes
2,445

Document number is not in table CDHDR. I have to pass document number in object value field to read the table CDHDR. Any suggestions

Read only

0 Likes
2,445

Hi,

In which field of table CDHDR you will pass the document no.and what filed value you require from that table.

Please provide complete information so that we can give proper guidance and inputs.

Regards,

AL

Read only

tom_darsy
Participant
0 Likes
2,445

Object value is a field in table CDHDR which contains the value

001400090000061692014. THe document number is contained in this field highlighted but it also has some other values in prefix.

Now i have field BELNR (Document number) which is from a different table and contains value 9000006169.

I need to read table CDHDR by passing field BELNR into Object value field.

Thanks


Read only

0 Likes
2,445

Hi Tom,

It seems completely related to ABAP so you can post your query in the below mentioned forum.

http://scn.sap.com/community/abap

Regards,

AL

Read only

former_member185177
Contributor
0 Likes
2,445

Hi Tom, 

Directly you cannot read the entries from a database table.  You need to fetch the data into internal table from there you can play around with the data using "READ" statement or any other what ever you want. 

So based on the inputs provided by you, you need to create an internal table and that table contains doc_no.  Fetch the data for required fields from CDHDR table using "SELECT" .

Loop at internal table.

then pass the document number value from field what you mentioned above example "001400090000061692014". Means please go through the below example code.

dATA: L_IDX TYPE SY-TABIX.

LOOP AT ITAB INTO WA_ITAB. 

      L_IDX = SY-TABIX.  

     WA_ITAB-DOCNO = FILED+8(10). 

      MODIFY ITAB FROM WA_ITAB INDEX L_IDX TRANSPORTING DOCNO.

ENDLOOP. 

Now you can use read statement using document number.  Because all the document numbers are now populated in the field DOCNO based on the above code. 

Hope it helps you. 

Cheers,

Krishna Chaitanya.

Read only

Former Member
0 Likes
2,445

Read table works on internal table which are local to your program.

so first use select statement to fetch data from Standard table into Internal table then use read table on internal tabl.

demo code:

data : LT_flight type table of Sflight(standard table) with header line.

select * from sflight into lt_flight.

read table lt_flight[] into lt_flight with key 'condition'.

.. Chans..

Read only

0 Likes
2,445

Hi Tom,

Give a select statement as below to CDHDR table

select * from cdhdr into table Itab where OBJECTID like  '%9000006169%'.

Now you have itab with records required, I a case where you have multiple records check for consistency of the data you got by validating.

regards

dp

Read only

0 Likes
2,445

This select will certainly work.  But since CDHDR is a very big table, you would prefer to use as much of the key as possible.  For FI documents, the Object is 'BELEG' or 'BELEGR'.  The object itself is a concatenation of the BKPF table key.  Therefor, in your case this means : 001400090000061692014

- 001 : Mandant (client)

- 4000 : company code

- 9000006169 : Fi document number

- 2014 : the fiscal year

If you select only on the document number using a like, you will not be sure you get the correct entries, as you will get all change documents for all companies and fiscal years where such a document number exists;  Therefor your statement should be :

select * from cdhdr into table Itab where OBJECTID = '001400090000061692014'

                                                                 and OBJECTCLAS = 'BELEG' (or 'BELEGR', depending on the document creation transaction).

You can easily create the objectid by concatenating the BKPF key fields.

Regards,

Freek

Read only

RaymondGiuseppi
Active Contributor
0 Likes
2,445

Which Object class are you trying to read ?

Be aware that BELNR is not the only primary key for many tables,  often a company code or a year are required top find the correct record, also don't forget to give internal format, do not drop leading zeroes. (e.g. client + comp. code + document number + year)

Regards,

Raymond

Read only

rosenberg_eitan
Active Contributor
0 Likes
2,445

Hi ,

There are some FM to help:

See form FORM test_1_1 .

Some code:

*----------------------------------------------------------------------*
FORM do_changedocu_key
  USING
    record  TYPE data
    tabname TYPE tabname
  CHANGING
    objectid TYPE cdobjectv .

  DATA: ev_tabkey TYPE cdpos_uid-tabkey .

  CALL FUNCTION 'CHANGEDOCU_KEY_ANY2CHAR'
    EXPORTING
      iv_struct_in     = record
      iv_tabname       = tabname
    IMPORTING
      ev_tabkey        = ev_tabkey
    EXCEPTIONS
      tabname_is_empty = 1
      nametab_error    = 2
      OTHERS           = 3.

  IF sy-subrc NE 0.
  ENDIF.

  objectid = ev_tabkey .

ENDFORM .                    "do_changedocu_key
*----------------------------------------------------------------------*
FORM do_dsp_change_document
  USING
    objectclas TYPE cdhdr-objectclas
    tabname    TYPE cdpos-tabname
    objectid   TYPE cdhdr-objectid .

  DATA: editpos TYPE TABLE OF cdred .

  CALL FUNCTION 'CHANGEDOCUMENT_READ'
    EXPORTING
      objectclass                = objectclas
      tablename                  = tabname
      objectid                   = objectid
    TABLES
      editpos                    = editpos
    EXCEPTIONS
      no_position_found          = 1
      wrong_access_to_archive    = 2
      time_zone_conversion_error = 3
      OTHERS                     = 4.

  CHECK sy-subrc EQ 0.

  CALL FUNCTION 'CHANGEDOCUMENT_DISPLAY'
    EXPORTING
      i_applicationid       = sy-repid
      flg_autocondense      = abap_false
      i_screen_start_line   = 10
      i_screen_start_column = 10
      i_screen_end_line     = 20
      i_screen_end_column   = 150
    TABLES
      i_cdred               = editpos.

ENDFORM .                    "do_dsp_change_document
*----------------------------------------------------------------------*
FORM test_1_1 .

  CONSTANTS: gc_tabname_1 TYPE tabname    VALUE 'BKPF' .
  CONSTANTS: gc_objclas_1 TYPE cdobjectcl VALUE 'BELEG' .

  DATA: st_bkpf TYPE bkpf .

* Some code to get bkpf record.....
* .....
* .....
* .....

  DATA: st_cdhdr TYPE cdhdr .

  PERFORM do_changedocu_key
     USING
       st_bkpf
       gc_tabname_1
     CHANGING
       st_cdhdr-objectid .

  PERFORM do_dsp_change_document
    USING
      gc_objclas_1
      gc_tabname_1
      st_cdhdr-objectid .

ENDFORM .                                                   "test_2_1
*----------------------------------------------------------------------*

Regards.

Eitan.