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

dbcnt in a loop.

Former Member
0 Likes
1,082

Hi Experts,

In a loop the following select query is there ,

Now I would like to get this out of loop , when I do that then how can I check dbcnt on a read statement.

LOOP AT it_output.

SELECT teindt seindt

INTO (it_output-eindt , it_output-edt)

FROM eket AS t

LEFT JOIN ekes AS s

ON sebeln = tebeln AND

sebelp = tebelp AND

s~ebtyp = 'AB'

WHERE t~ebeln = it_output-ebeln

AND t~ebelp = it_output-ebelp.

IF sy-dbcnt = 1.

MODIFY it_output.

ELSE.

INSERT it_output.

ENDIF.

ENDSELECT.

ENDLOOP.

Please suggest me whether this can be possible .

Thanks in advance.

8 REPLIES 8
Read only

Former Member
0 Likes
1,012

I think you should first make sure of your logic. Why are you trying to insert a record into a table when it is unchanged? Why are you trying to insert a record into a table that you are looping at?

Rob

Read only

Former Member
0 Likes
1,012

this coding is very weird.

the sy-dbcnt is set by the select - endselect loop and not by the loop on the internal table.

And the idea to insert lines into the table which is used in the loop is highly dangerous.

I am quite sure that you can create endless loops with that.

if your on a basis newer than 6.10, then you can insert directly into the table which is

used in the FOR ALL ENTRIES.

Try this one, maybe it works. I know that it works in general but not every detail.


SELECT t~eindt as eindt s~eindt as endt
             INTO INTO CORRESPONDINg FIELDS OF TABLE it_output
             FROM eket AS t
             LEFT JOIN ekes AS s
             ON s~ebeln = t~ebeln AND
             s~ebelp = t~ebelp AND
             s~ebtyp = 'AB'
             FOR ALL ENTRIES IN it_output.
             WHERE t~ebeln = it_output-ebeln
            AND t~ebelp = it_output-ebelp.

Siegfried

Read only

0 Likes
1,012

is the following solution is right.

data: dbcnt type sy-dbcnt.

if not it_output is initial.

SELECT teindt seindt

INTO table it_11

FROM eket AS t

LEFT JOIN ekes AS s

ON sebeln = tebeln AND

sebelp = tebelp AND

s~ebtyp = 'AB'

WHERE t~ebeln = it_output-ebeln

AND t~ebelp = it_output-ebelp.

if sy-dbcnt = 1.

dbcnt = 1.

endif.

endif.

LOOP AT it_output.

read table it_11 into wa_it11 with key ebeln = it_output-ebeln

ebelp = it_output-ebelp.

if sy-subrc = 0 and dbcnt =1.

MODIFY it_output.

ELSE.

INSERT it_output.

ENDIF.

ENDLOOP.

is this going to work.

Read only

0 Likes
1,012

You don't need to use the sy-dbcnt.

In your code, after READ you also need to change the values of lt_output, before insert or modify.

Something is strange in your code... FAE lt_output.... and want's to save on lt_output ?!?!?

Edited by: Fernando Ros on Mar 24, 2009 5:39 PM

Edited by: Fernando Ros on Mar 24, 2009 5:43 PM

Read only

Former Member
0 Likes
1,012

Hi,

Is this something related to you early post ??

I think that what you asked is this:

READ TABLE lt_xpto ASSIGNING <fs_xpto>
                   WITH KEY mblnr = (your mblnr value)
                            mjahr = (your mjahr value)
                   BINARY SEARCH.
IF sy-subrc EQ 0.   "<-- cheking the result of READ
  * exist on lt_xpto... so MODIFY
ELSE.
  * doesn't exist on lt_xpto... so INSERT
ENDIF.

Please considerar a code review, since if you are changing your code to fill a internal table, doesn't make sense update one by one.

Regards,

Fernando Da Ros

Read only

Former Member
0 Likes
1,012

you should try what I have written it is faster than any nested loop.

Otherwise the your loop has still the same problem as before, it is logically incorrect in the

order:

You must process the new table completely, not the output table!

And it is weird to input into loop table.


   SORT it_output BY eindt edt.

LOOP AT itab INTO wa.
   READ TABLE it_output
             TREANSPORTING NO FIELDS
             WITH KEY eindt = wa-eindt
                              eindt = wa-edt.
              BINARY SEARCH.
    idx = sy-tabix.
   IF ( SY-SUBRC eq 0 ).
      MODIFY it_output FROM wa INDEX idx.
   ELSE.
      INSERT wa INTO it_output INDEX idx
   ENDIF

ENDLOOP:

please check the variable names,

Siegfried

Read only

Former Member
0 Likes
1,012

Ameer,

I think this is what you are trying to do:

1. You have an internal table it_output that has 2 fields to be populated from the eket and ekes tables viz. it_output-eindt and it_output-edt respectively

2. If the database table join returns more than one entries ... the it_output table needs to have one entry for every corresponding database result and that is why you try to insert the new records in the table.

If the above is true ... this is why you cannot do what is written above:

1. eindt and edt values are not available in your it_output table yet!!!

2. Besides when you try to read from it_output or the other table with the keys as eindt and edt ... they are dates and they can very well repeat for the different combinations of ebeln and ebelp!!!!!!

just use the index as mentioned and do this

****************************************************************************

DATA: BEGIN OF ls_eket_ekes,

ebeln TYPE eket-ebeln,

ebelp TYPE eket-ebelp,

eindt TYPE eket-eindt,

edt TYPE ekes-eindt,

END OF ls_eket_ekes.

DATA: lt_eket_ekes LIKE SORTED TABLE OF ls_eket_ekes with non-unique key ebeln ebelp .

**this will be your select statement to read from the database

SELECT t~ebeln

t~ebelp

t~eindt

s~eindt AS edt

INTO CORRESPONDING FIELDS OF TABLE lt_eket_ekes

FROM eket AS t

LEFT JOIN ekes AS s

ON tebeln EQ sebeln

AND tebelp EQ sebelp

AND s~ebtyp EQ 'AB'

FOR ALL ENTRIES IN it_output

WHERE t~ebeln EQ it_output-ebeln

AND t~ebelp EQ it_output-ebelp.

**Now do this to move data

DATA: lv_first TYPE flag.

sort it_output by ebeln ebelp.

LOOP AT lt_eket_ekes INTO ls_eket_ekes.

AT NEW ebelp.

lv_first = 'X'.

ENDAT.

IF lv_first EQ 'X'.

READ TABLE it_output

WITH KEY ebeln = ls_eket_ekes-ebeln

ebelp = ls_eket_ekes-ebelp

BINARY SEARCH.

idx = sy-tabix.

IF sy-subrc EQ 0.

it_output-eindt = ls_eket_ekes-eindt.

it_output-edt = ls_eket_ekes-edt.

MODIFY it_output

TRANSPORTING eindt edt

index idx.

ENDIF.

CLEAR lv_first.

ELSE.

it_output-eindt = ls_eket_ekes-eindt.

it_output-edt = ls_eket_ekes-edt.

insert it_output index idx.

ENDIF.

ENDLOOP.

****************************************************************

Let me know if this works!

Regards,

Saika

Edited by: Saika Shaikh on Mar 26, 2009 9:40 AM

Read only

Former Member
0 Likes
1,012

no, that is not what I have written!

Your coding is again full of bugs. Why don't you just follow what I have written, if you don't know

much about ABAP programming of internal tables. I don't understand.

The table it_output is sorted otherwise the BINARY SEARCH does not work properly.

=> APPEND is forbidden !!!!!!!!!!!!

The MODIFY WHERE is the second big bug. It loop over the whole table.

You must use the two operations with index, as I have written !

And forget the last SORT, the table is sorted once, and must stay sorted.

Simplify the whole thing by using a sorted table for it_output, if possible.

Then yon can't does anything wrong.

Siegfried