2013 Sep 19 7:37 AM
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 1 | Header 2 | Generated No |
|---|---|---|
| 1001 | Test1 | 1 |
| 1002 | Test2 | 1 |
| 1003 | Test3 | 1 |
| 1004 | Test4 | 2 |
| 1005 | Test5 | 2 |
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.
2013 Sep 19 7:48 AM
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.
2013 Sep 19 7:48 AM
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.
2013 Sep 19 8:00 AM
Dear Nivedita,
Thanks for your quick reply. But i did not get you.
2013 Sep 19 8:09 AM
Hi,
Could you please share the code you are using to upload data from internal table.
Thanks.
Ankita
2013 Sep 19 8:46 AM
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. "
2013 Sep 19 9:02 AM
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.
2013 Sep 19 9:17 AM
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. "
2013 Sep 19 9:46 AM
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.
2013 Sep 19 8:10 AM
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
2013 Sep 19 9:04 AM
2013 Sep 19 12:33 PM
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
2013 Sep 19 1:02 PM
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.