How to generate unique document number ?

Hi All,
We need to generate Document Number where there should be any gap, so that we cannot use sequence. For example : Invoice Number.
one possible approach like : Select MAX(InvoiceNumber)+1 from invoice, what if that table has millions of rows, is this reliable enough ?
How to avoid two user do the same query at same time that lead to dupplicate number ?
Anybody has done it this way in production? or any other approach ?
Thank you for your info,
xtanto

I strongly second Justin's strong suggestion about re-considering the requirement.
Be aware that for any of the schemes suggested to have a chance of working, all access to the table must be through stored procedures that use a SELECT FOR UPDATE on the counter table. No user in the database can have insert privileges on the document table except the owner. The owner's password should be kept in a locked safe and be unknown to anyone. If for some reason the owner needs access to the database, then remove the password from the safe, do what needs to be done, then get some guy off the street to set a new password, write it down and lock it in the safe again.
If you really need a gap-free number, it needsto be another column, separate from the PK. Use a sequence to generate the pk gaps and all, then create a procedure that runs periodically to update the gap free column. Soemthing like:
SQL> CREATE PROCEDURE fix_gap_free AS
  2     l_v NUMBER;
  3     TYPE id_tbl IS TABLE OF number INDEX BY BINARY_INTEGER;
  4     l_ids  id_tbl;
  5     l_id2s id_tbl;
  6  BEGIN
  7     SELECT MAX(NVL(id2,0)) INTO l_v FROM t;
  8     SELECT id, ROW_NUMBER() OVER (ORDER BY id)
  9     BULK COLLECT INTO l_ids, l_id2s
10     FROM t
11     WHERE id2 IS NULL;
12
13     FORALL i IN 1 .. l_ids.COUNT
14        UPDATE t
15        SET id2 = l_id2s(i) + l_v
16        WHERE id = l_ids(i);
17     COMMIT;
18  END;
19  /
Procedure created.
SQL> SELECT * FROM t;
        ID        ID2
         1
         2
         5
         7So, id2 is going to be the gap-free column.
SQL> exec fix_gap_free;
PL/SQL procedure successfully completed.
SQL> SELECT * FROM t;
        ID        ID2
         1          1
         2          2
         5          3
         7          4So first pass is fine. Now lets add a few rows, so the table now looks like:
SQL> SELECT * FROM t;
        ID        ID2
         1          1
         2          2
         5          3
         7          4
         8
         9
        25and generate more gap free number:
SQL> exec fix_gap_free;
PL/SQL procedure successfully completed.
SQL> SELECT * FROM t;
        ID        ID2
         1          1
         2          2
         5          3
         7          4
         8          5
         9          6
        25          7If there may be deletions in the table, and you need to maintain the gap free nature, then just skip the WHERE id2 IS NULL predicate.
John

Similar Messages

  • How to generate new perner number using Abap Code in pa0002. what is the fu

    how to generate new perner number using Abap Code in pa0002. what is the function Module used.

    hi
    define internal number ranges for ur implementation then when u run PA 40  do not input any employee number , system will generate the employee number automatically.
    Regards
    sameer

  • How to get payment document number from paid column in monthly invoice

    Dear experts
    I use SBO japan version and i need to know how to get payment document number from paid column in monthly invoice ?
    in table MIN1, only contain invoice and credit note document number, there is no payment document number
    thank you for your help
    Best Regards
    JeiMing

    Dear Gordon
    Yeah, you are right, i can use field MIentry in RCT2
    thank you
    Best regards
    jeiming

  • How to get the document number for a ware house order.

    Hello gurus,
    how to get the document number for a ware house order. [if GI is posted refering that WH order] .. is there any report?
    Thanks in advance

    There are several options.  When you post a Goods Movement, you can use LB12 -display Transfer Requirement for material document or LB11 Display TR for material.
    Depending on how your system is set up you may have gotten a Transfer Order automcatically.  In this case you can look at LT24 - Transfer Order for material.

  • How to get/view document number for KP06 Planning upload

    Hi,
    I have uploaded excel for Cost center/cost element Planning in KP06 using exccel planning.Now i want the document number which for which i have posted in KP06-Plan values using excel upload.
    Kindly advice me how to get the document number for CO-OM(Cost center/Cost element planning) in KP06?
    Thanks
    Supriya

    Hello,
    Please execute report KSBL. It should be useful.
    Regards,
    Ravi

  • How to encrypt the document number in URL

    Hi All,
    I am working on a project where I need to send a URL to the user in his email, the URL contains the document number in it. when the user clicks on that URL a webdynpro application will open with data prepopulated based on the document number, now what I want his how can encrypt the document number in the URL when sending it to the user. so basically the user who gets the URL should not be able to see the document number in the URL.
    Please advise.
    Thanks,
    Rajat Garg

    Thanks again for the reply and now I am able to encrypt and decrypt my document number... one more question please : will it be possible to chnage the whole URL to some basic message type URL for eg:
    let's say our URL is "http://testdoc/post?mssg" and I want to change this to as "OPEN DOCUMENT" and when user clicks on ""OPEN DOCUMENT" it will still direct to the original destination that is our original URL.
    I have been told that we don;t want to maintain custom table until and unless it's our last choice.
    Thanks,
    Rajat

  • How to control Held documents number ranges

    Hi Gurus.,
    pls tell me How to control Held documents number ranges.,
    its urgent .one of my client asking separate number ranges for held documents
    with regards
    ramesh

    Dude,
    I understand you are refering Hold Document not Park document. Becoz Park document use the same number range which is used for normal document posting. However, for Hold Document Number ranges, i advice you to liaise with your Abaper for BADI/Exit/ or etc etc.....for maintaing a specific number range or validation of number given.
    pls assign point if useful

  • How to get Accounting document number from billing document number

    Hi,
    How to get Accounting document number from billing document number i.e. from VBAK- VBELN.
    Cheta Pant

    Hi,
    VBAK is the Sale Order header table, Billing document table is VBRK.
    Sales tables (VBAK,VBAP,VBRP,VBRK) do not store accounting document numbers.
    You do other way around. Just go to SE16 and enter the Billing document number in field Billing document VBELN in BSEG or BSID or BSAD table and execute, you will find the accounting document number.
    If you just want collect this information once then you can use table BSEG, but if you think to develope a report then use table BSID and BSAD.
    Regards,
    Chintan Joshi

  • How to find the document number generated by F-53 Tcode.

    Hi expert,
    I am using user exit EXIT_XFMPR1_001, at this point I want to export document number, vendor number , vendor name, Vendor address, invoice number, invoice date, invoice amount, WHT, and net amount.
    I get these, than I will transport it into text format file.
    Thanks
    Syed Tayab Shah

    Hi,
    What is ur requirement? why do you need to download/transfer to txt format?
    Still want to do it? then try INDX table to export your contect.
    And then read the table later...
    If the hint is usefulu2026 Say thanks by rewardu2026.
    Regards,
    Prabhu Rajesh

  • How to generate unique numbers

    hi all,
    I want to generate unique numbers always starting from 1 using a single sql query and I dont want to use a sequence and i dont think rownum will be helpful for generating unique numbers in all cases . how to do this ?
    thanks & regards,
    kumar.

    If two users get the MAX from temp, they would be updating with the same value for two different rows.If TEMP is a genuine temporary table, this is not an issue, because the data in a temporary table is only visible within a session.
    The big problem with this approach is the mutating table one: we cannot issue the requisite select statement from a BEFORE INSERT trigger. So this solution is only going to work with a view and an INSTEAD OF INSERT trigger.
    Another, more workable, solution is to use a code control table. This can be (should be) another temporary table, with one row holding one column - next value for TEMP.ID. Normally we would rule out such a solution because it serializes access to the inserted table, but as this is only on a per session basis, it doesn't matter.
    CREATE OR REPLACE TRIGGER tmp_bir BEFORE INSERT ON temp1 FOR EACH ROW
    DECLARE
      ln NUMBER;
    BEGIN
      SELECT nvl(code1.next_id,1) INTO ln
      FROM code1;
      :NEW.id := ln;
      UPDATE code1
      SET    next_id := ln+1;
    END;Beware: coded freehand, so may need debugging.
    Cheers, APC

  • How to fetch accounting document number from known material document number

    Hi,
    Using MIGO transaction, by giving mblnr(material document number) as input, I get accounting document number by clicking FI document.I have to add this accounting document number in my report for corresponding known mblnr(material document number) values.
    My question is how to fetch the accounting document number present in MIGO to add in my report program.
    I used the xblnr(Reference Document Number) which is present both in mkpf and bkpf tables to fetch values.
    I extracted xblnr values with known mblnr values from mkpf (Header: Material Document table).
    and then extracted belnr(accounting document number) from bkpf (Accounting Document Header table) by using xblnr values.
    But the query is running for a long time.
    Is there any other method  to extract the values in a simpler way.
    Kindly advise.
    Thanks and Regards,
    Sanjeev

    I had the values of xblnr and some other fields such as mblnr, budat etc in wi_item table.
    I created a new internal table i_xblnr and got down those values.
    And then created a new internal table i_belnr and tried to get values of belnr in it.
    The code I wrote is given below:
    IF not wi_item[] is initial.
    loop at wi_item.
       at new xblnr.
        ws_xblnr-xblnr = wi_item-xblnr.
         append ws_xblnr to i_xblnr.
       endat.
    endloop.
      select belnr xblnr from bkpf into table i_belnr for all entries in i_xblnr where xblnr = i_xblnr-xblnr.
    ENDIF.
    Kindly look after it.Thank you.
    Regards,
    Sanjeev

  • How to catch accounting Document Number in SE37 for  BAPI_ACC_DOCUMENT_POST

    HI Frds,
    My ReQ is i have to post Customer Invoice from Legacy to ECC through PI interface. So i am using one BAPI , ' BAPI_ACC_DOCUMENT_POST''. I had customized this BAPI by adding 2 fields (1.zstatus, 2.zsaprefno) as i have to send the status(1. Success/Fail, 2.Account Document Number) back to Legacy.
    For PI i have to give the BAPI structure. So when i am executing in SE37 zstatus field is catching the value as 'Success'. But the ZSAPREFNO is not catching the account document number. Bez we have to execute BAPI_TRANSACTION_COMMIT after this BAPI(in test sequence from Menu bar). So directly updating in Database tables.
    when i was excuting in SE38 the Accounting Document Number is returned in RETURN parameter of MESSAGE_V2 as we are checking the return parameter after BAPI_TRANSACTION_COMMIT.
    when i am trying in SE37 its not catching the value in MESSAGE_V2(catching as $).  After COMMITing we have no option to check the RETURN parameter.
    So, any idea how to get the Account Document Number  in SE37.......Or any other way to do..........
    Edited by: priya tavanam on Sep 3, 2010 7:02 AM

    Hi,
    if standard BAPI did not return the Acc. document number before BAPI_COMMIT then create wrapper FM
    1) call actual BAPI and BAPI_COMMIT with in that.
    2) retrun the account document back.
    otherwise you can try sync proxy as well.
    Regds,
    Suresh

  • How to include the document number in SM20 or SLG1 ?

    Hi every one ,
    i want to dispaly the actions made by the user so i sed transaction SM20 and SLG1 but they doesn't display the Document number created by the user ..
    How can i include the document number in e result of SM20 and SLG1 ?
    Thanks in advance

    I have to configure SM19 first then SM20
    THanks

  • How to generate unique filenames??

    i need to be able to generate unique files from a servlet..
    my initial instinct was to use the seesion id as part of the filename, however as this file will be embedded in the responding html, this is not safe, as the user will only have to look at the html source to view a session id value.
    i am now considering to use the date/time of the creation of a session as the unique identifier for the file, however this will not work if two sessions can be created at the same time.
    my question thefore is if no two sessions can have the same date and time?
    if not.. can anyone give me an idea as how to produce unique filenames?

    If you are actually creating a file, why not usethe
    java.io.File.createTempFile() method?how does that help with prducing a unique value to use
    as the name of a file?
    with that method i still need to supply the filename
    as one of its arguments!
    No you don't. You provide a prefix and suffix and it fills in the middle with something guaranteed to be unique, in the directory you specify. Here is the javadoc:
    createTempFile
    public static File createTempFile(String prefix,
    String suffix,
    File directory)
    throws IOException
    Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name. If this method returns successfully then it is guaranteed that:
    1. The file denoted by the returned abstract pathname did not exist before this method was invoked, and
    2. Neither this method nor any of its variants will return the same abstract pathname again in the current invocation of the virtual machine.
    This method provides only part of a temporary-file facility. To arrange for a file created by this method to be deleted automatically, use the deleteOnExit() method.
    The prefix argument must be at least three characters long. It is recommended that the prefix be a short, meaningful string such as "hjb" or "mail". The suffix argument may be null, in which case the suffix ".tmp" will be used.
    To create the new file, the prefix and the suffix may first be adjusted to fit the limitations of the underlying platform. If the prefix is too long then it will be truncated, but its first three characters will always be preserved. If the suffix is too long then it too will be truncated, but if it begins with a period character ('.') then the period and the first three characters following it will always be preserved. Once these adjustments have been made the name of the new file will be generated by concatenating the prefix, five or more internally-generated characters, and the suffix.
    If the directory argument is null then the system-dependent default temporary-file directory will be used. The default temporary-file directory is specified by the system property java.io.tmpdir. On UNIX systems the default value of this property is typically "/tmp" or "/var/tmp"; on Microsoft Windows systems it is typically "c:\\temp". A different value may be given to this system property when the Java virtual machine is invoked, but programmatic changes to this property are not guaranteed to have any effect upon the the temporary directory used by this method.
    Parameters:
    prefix - The prefix string to be used in generating the file's name; must be at least three characters long
    suffix - The suffix string to be used in generating the file's name; may be null, in which case the suffix ".tmp" will be used
    directory - The directory in which the file is to be created, or null if the default temporary-file directory is to be used
    Returns:
    An abstract pathname denoting a newly-created empty file
    Throws:
    IllegalArgumentException - If the prefix argument contains fewer than three characters
    IOException - If a file could not be created
    SecurityException - If a security manager exists and its SecurityManager.checkWrite(java.lang.String) method does not allow a file to be created
    Since:
    1.2

  • How to generate external network number based on WBS element number

    Hi all(experts)
    I am new to PS and I have a requirment.
    <u>To derive network no. from WBS identifier.</u>
    Enhancement is CNEXOO12
    Function module is EXIT_SAPLCOKO_002(data transfer for Network number).
    I wnat to know how to use the above enhancement to generate external network number based on the WBS element  number. We have to take the Project number and extract the part of the name from it and then assign it to NETWORK and ACTIVITIY under it. The above also depends upon the project type.
    Please let me know the fields\structures\tables that are needed and available during runtime in this Function module exit. If possible with some examples.
    Thanks for your time.
    SH

    Hi Karthik
    Thanks for sending me the table relations.
    Can you please help me with the following.
    1) Can you please expand the table relations with an example because when I am doing a relation I am not able to get the AUFNR fromAUFK-->PSPEL because the there are four levels of WBS in the Project and if I plug the first level WBS from the PRPS there are no table entries found. if I put directly the WBS(fourth level X-yyyyyyy95.xxxx10.10.2) in the AUFK-PSPEL table it works and I get the AUFNR.
    2) Can I do retrieve the network number from the sales order(KDUAF).
    Please suggest.
    By enhancing this CNEX0012(EXIT_SAPLCCOKO_002) which transactions in PS are affected.
    Please reply at your earliest.
    As always thanks for your time and your help
    SH.

Maybe you are looking for

  • Fdpstp failed due to ora-12899 value too large for column

    Hi All, User facing this problem while running the concurrent program. the program is complted but with rhis error. fdpstp failed due to ora-12899 value too large for column Can any one tell me the excat solution for this? RDBMS : 10.2.0.3.0 Oracle A

  • How to pass '&' as a parameter in IDOC Status avoiding it to be replaced

    Hi,    This is the issue I have. Say an IDOC got status 51 when trying to post it. One of the messages to be appended in table EDIDS is: Message Class   Message Number    Text ZZ                      001                       This is parmeter1  & and

  • Problem adding songs to library

    First of all, a couple of weeks ago I got the dreaded "corrupt library file" error message. Tried everything I could to get it back with no luck. I finally decided that I would just start from scratch and re-add all of my music to the library, and in

  • Connections: Disconnected Database get automatically reconnected

    PC: XP SP2, Intel Pentium 4. SQL Developer Version 1.1.3 Build Main 27.66 Lets say i connect to 3 databases. Then I disconnect from 2. I use the remaining connection to edit a package. After closing the package I see that my recently disconnected con

  • Installing US version of CS4 on a UK PC and future upgrade options

    I'm sure this has been asked countless times before on these boards, so please accept my apologies in advance! My question is this: if I were to buy a US version of Adobe CS4 Design Premium for my UK Windows Vista PC (I have a friend in the US who ca