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

Problem with generated number in custom table.

former_member308418
Participant
0 Likes
1,284

Dear All,

I have a  z table in which I am uploading data from my internal table. My requirement is each time my table is updated a unique number will generate. I am fine with it.When I am passing same data all the fields are just updated , i, e no duplicate data is inserted but the unique number changes. I am giving an idea about my problem below.

Header 1Header 2Generated No
1001Test1 1
1002Test21
1003Test31
1004Test42
1005Test52

This is the table. Here Header1 is primary key field. Each time new data is inserted Generated no increases. But if I give the existing data like 1001, 1002,1003 again, then all the columns remain same but Generated No increase to 3. But it should be 1, i, e i want no change whenever giving same data. I am using number range for the generated no. Plz help me to solve the problem.

With best regards.


12 REPLIES 12
Read only

KiranJ
Active Participant
0 Likes
1,208

Hi, First check the data data base table if it already exists dont call number range object so data is already existing it will not update anything and number range will not change.

Read only

Former Member
0 Likes
1,208

Hi, You should use a group by statement with the key fields and then after group by only the row number is to be generated.

Read only

0 Likes
1,208

Dear Nivedita,

Thanks for your quick reply. But i did not get you.

Read only

Private_Member_3759
Participant
0 Likes
1,208

Hi,

Could you please share the code you are using to upload data from internal table.

Thanks.

Ankita

Read only

0 Likes
1,208

Hi,

My code snippet for uploading data into Z table is below.

FORM insert_data.

   CALL FUNCTION 'NUMBER_GET_NEXT'
     EXPORTING
       nr_range_nr             = '01'
       object                  = 'ZRANGE'
     IMPORTING
       number                  = number
     EXCEPTIONS
       interval_not_found      = 1
       number_range_not_intern = 2
       object_not_found        = 3
       quantity_is_0           = 4
       quantity_is_not_1       = 5
       interval_overflow       = 6
       buffer_overflow         = 7
       OTHERS                  = 8.
   IF sy-subrc <> 0.
     MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
        WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
   ENDIF.

  LOOP AT it_tab INTO wa_tab.
     wtable-gnumber = number.
     wtable-matnr = wa_tab-matnr.
     wtable-maktx = wa_tab-maktx.
     wtable-ernam = wa_tab-ernam.

     APPEND wtable TO itable.
     MODIFY ztesttable1 FROM TABLE itable.
     ENDLOOP.

ENDFORM.                    "

Read only

0 Likes
1,208

But that is what you programmed, so the program does what you tell it to do.

if you want to update the number only when key is not available you should include a check first to see if the key exist already and only when it doesn't you fill wtable-gnumber (should not be a key field).

LOOP AT it_tab INTO wa_tab.

    read table ztable with key wa_tab-ztable_key.

    if sy-subrc <> 0.  "entry does not exist is  ztable yet

        wtable-gnumber = number.

    endif.

     wtable-matnr = wa_tab-matnr.

     wtable-maktx = wa_tab-maktx.

     wtable-ernam = wa_tab-ernam.

     APPEND wtable TO itable.

     MODIFY ztesttable1 FROM TABLE itable.

* and clear the work area, otherwise it will not work (number will stay in field wtable-gnumber )

     CLEAR wa_tab.

ENDLOOP.

Read only

0 Likes
1,208

Hi,

You Can try like this also..

Data :  wa_ztesttable1 type ztesttable1.

FORM insert_data.

  LOOP AT it_tab INTO wa_tab.

     select single gnumber

                             matnr
                              maktx
                              ernam

                              from ztesttable1

                              into wa_ztesttable1

                              where matnr = wa_tab-matnr.

if sy-subrc ne 0.

   CALL FUNCTION 'NUMBER_GET_NEXT'
     EXPORTING
       nr_range_nr             = '01'
       object                  = 'ZRANGE'
     IMPORTING
       number                  = number
     EXCEPTIONS
       interval_not_found      = 1
       number_range_not_intern = 2
       object_not_found        = 3
       quantity_is_0           = 4
       quantity_is_not_1       = 5
       interval_overflow       = 6
       buffer_overflow         = 7
       OTHERS                  = 8.
   IF sy-subrc <> 0.
     MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
        WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
   ENDIF.

     wtable-gnumber = number.
     wtable-matnr = wa_tab-matnr.
     wtable-maktx = wa_tab-maktx.
     wtable-ernam = wa_tab-ernam.

     APPEND wtable TO itable.
     MODIFY ztesttable1 FROM TABLE itable.

else

     wtable-gnumber = wa_ztesttable1-gnumber.

     wtable-matnr = wa_tab-matnr.
     wtable-maktx = wa_tab-maktx.
     wtable-ernam = wa_tab-ernam.

     APPEND wtable TO itable.
     MODIFY ztesttable1 FROM TABLE itable.

ENDLOOP.
ENDFORM.                    "

     

Read only

0 Likes
1,208

Sorry it should be:

LOOP AT it_tab INTO wa_tab.

    read table ztable into wa_ztable with key wa_tab-ztable_key.

    if sy-subrc <> 0.  "entry does not exist is  ztable yet

        wtable-gnumber = number.

    else.

        wtable-gnumber = wa_ztable-gnumber.

    endif.

     wtable-matnr = wa_tab-matnr.

     wtable-maktx = wa_tab-maktx.

     wtable-ernam = wa_tab-ernam.

     APPEND wtable TO itable.

     MODIFY ztesttable1 FROM TABLE itable.

* and clear the work area, otherwise it will not work (number will stay in field wtable-gnumber )

     CLEAR wa_tab.

ENDLOOP.

Read only

SujeetMishra
Active Contributor
0 Likes
1,208

Hi,

Step-1 First you have all the data in one final internal table.

Step-2 Sort final Internal table by your Primary field.

Step-2 Loop your final internal table.

Step 3. Generate 'Generated No' field in Event 'AT NEW' and here check your primary key field to check             new record every time.

Regards,

Sujeet

Read only

Former Member
0 Likes
1,208
Hello,
Please try this.

FORM insert_data.

  LOOP AT it_tab INTO wa_tab.

    SELECT SINGLE * FROM ztesttable1 WHERE matnr = wa_tab-matnr.

    IF sy-subrc = 0.

      wtable-gnumber   = wa_tab-gnumber.
      wtable-matnr     = wa_tab-matnr.
      wtable-maktx     = wa_tab-maktx.
      wtable-ernam     = wa_tab-ernam.

    ELSE.

      CALL FUNCTION 'NUMBER_GET_NEXT'
        EXPORTING
          nr_range_nr             = '01'
          object                  = 'ZRANGE'
        IMPORTING
          number                  = number
        EXCEPTIONS
          interval_not_found      = 1
          number_range_not_intern = 2
          object_not_found        = 3
          quantity_is_0           = 4
          quantity_is_not_1       = 5
          interval_overflow       = 6
          buffer_overflow         = 7
          OTHERS                  = 8.
      IF sy-subrc <> 0.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
           WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      ELSE.

        wtable-gnumber   = number.
      ENDIF.
    ENDIF.

    APPEND wtable TO itable.

    MODIFY ztesttable1 FROM TABLE itable.

  ENDLOOP.

ENDFORM.
Cheers
Read only

Former Member
0 Likes
1,208

HI,

Take your data element for generated no as GERNR and make it primary key this will resolve your problem(if you are maintaining your Ztable via se11 or se16 or sm30) .

One more way to authenticate the serial no increment is to obtain screen number for the TMG and explicitly use the FM NUMBER_GET_NEXT to generate a new counter every time, with this Fm you can set your number range as well.

Hope this will solve your issue.

Best Regards-

Tarun

Read only

Former Member
0 Likes
1,208

Hi,

create a work area of that same z table type. (wa_ztab)

& suppose u r using wa_tab for inserting.

loop at it_ztab into wa_ztab.

read table it_ztab into wa_ztab with key wa_ztab-header1.

if sy-subrc NE 0.

CALL FUNCTION 'NUMBER_GET_NEXT'

     EXPORTING

       nr_range_nr             = '01'

       object                       = 'XXXX'

     IMPORTING

       number                    = num

wa_ztab-genareted_no = num.

append wa_tab itno it_ztab.

else.

  read table it_ztab into wa_ztab with key wa_ztab-header1.

  wa_tab-genareted_no = wa_ztab-genareted_no

  append wa_tab itno it_ztab.

endloop.