Concurrent Insert .....MultiUser Issue

Last post 04-24-2005 10:20 AM by AdamMachanic. 6 replies.

Sort Posts:

  • Concurrent Insert .....MultiUser Issue

    04-16-2005, 9:52 AM
    • Member
      157 point Member
    • itismesa
    • Member since 10-18-2003, 7:26 AM
    • Posts 45
    Hi

    I am developing program in VB.NET  that would be used by 10 users concurrently. Each of them can save a receipt to the table RECEIPT in SQL Server 2000. Every time a new receipt is added should take a number equal to the last receipt number plus One.
     
    first I got the last Receipt number with
    SELECT MAX(ID) LAST_ID  FROM RECEIPT
    and add one to it
     
    then
    I execute Non Query command to add the new receipt with the new ID
    INSERT INTO RECEIPT (ID,CUSTOMERID, .....etc
     
     
    The problem when two of the users try to add receipts at the same time then there is a chance that a primary key conflict. would take place and will get error. How can I handle this matter and make sure even if the ten users tried to add receipt at the same time, it will work

    Regards
  • Re: Concurrent Insert .....MultiUser Issue

    04-16-2005, 10:35 AM
    • All-Star
      23,295 point All-Star
    • douglas.reilly
    • Member since 11-19-2002, 9:19 PM
    • Brick, NJ USA
    • Posts 4,647
    Create an IDENTITY column in the table, use that, a number that will be autoincremented safely, to ensure that all rows have a unique number.
    Starting with ASP.NET 2.0? Look at:
    Programming Microsoft Web Forms
    My Blog
  • Re: Concurrent Insert .....MultiUser Issue

    04-16-2005, 4:30 PM
    • Member
      157 point Member
    • itismesa
    • Member since 10-18-2003, 7:26 AM
    • Posts 45

    first i would like to thank u

    second if i use the IDENTITY column, How can I know the receipt number of the receipt that just has been added? Is there away to track the last IDENTITY added

     

     

  • Re: Concurrent Insert .....MultiUser Issue

    04-16-2005, 11:20 PM
    • Contributor
      5,962 point Contributor
    • Xanderno
    • Member since 06-17-2002, 3:24 PM
    • Plano, TX
    • Posts 1,181
    Sure, several ways.  For instance, SELECT SCOPE_IDENTIY() will get you an identity value that's just been added within the current connection scope. 
  • Re: Concurrent Insert .....MultiUser Issue

    04-18-2005, 1:42 AM
    • Member
      157 point Member
    • itismesa
    • Member since 10-18-2003, 7:26 AM
    • Posts 45

    Thank u Xandor

    unfortunatly, I found out that i can not use the Identity because the company has many branches and the primary key to be composit key of serial number and branch number. for example

    Ser_No        Br_No

    1                  1

    2                   1

    3                    1

    1                    2

    2                     2

    3                      2

    ......etc

    so i should assign a unique number within every branches

    any help pls

  • Re: Concurrent Insert .....MultiUser Issue

    04-23-2005, 5:56 PM
    • Participant
      1,932 point Participant
    • pcdanno
    • Member since 04-24-2003, 7:50 AM
    • Chicago, IL
    • Posts 399
    Here's what I would do assuming that you are inserting rows into your database via a DataSet
    1) Create an autoincrement column in your table that corresponds to your SerialNumber
    2) In the RowUpdated event of your DataAdapter add code to retrieve the highest SerialNumber from your table (SELECT SCOPE_IDENTITY)
    3) Set the value of your AutoIncrement column to that value
    4) Then perform your insert

    Getting the value via the RowUpdated event will ensure that you will always get the highest serialnumber prior to performing an Insert

    Good Luck
  • Re: Concurrent Insert .....MultiUser Issue

    04-24-2005, 10:20 AM
    • Participant
      995 point Participant
    • AdamMachanic
    • Member since 04-14-2005, 10:00 AM
    • Boston, MA
    • Posts 199
     itismesa wrote:

    unfortunatly, I found out that i can not use the Identity because the company has many branches and the primary key to be composit key of serial number and branch number. for example


    You can (and should) still use an IDENTITY column, even given this requirement.  You can easily generate the row numbers on a per-branch basis at read time, based on the IDENTITY column.  For instance:

    DECLARE @x TABLE
    (
    BranchId INT,
    IdCol INT IDENTITY(1,1)
    )

    INSERT @x (BranchId) VALUES (1)
    INSERT @x (BranchId) VALUES (1)
    INSERT @x (BranchId) VALUES (2)
    INSERT @x (BranchId) VALUES (1)
    INSERT @x (BranchId) VALUES (3)
    INSERT @x (BranchId) VALUES (2)
    INSERT @x (BranchId) VALUES (3)

    SELECT *
    FROM @x

    SELECT x1.BranchId, x1.IdCol, COUNT(*) BranchRowNum
    FROM @x x1
    JOIN @x x2 ON x2.BranchId = x1.BranchId
    AND x2.IdCol <= x1.IdCol
    GROUP BY x1.BranchId, x1.IdCol
    ORDER BY x1.BranchId, x1.IdCol
    Adam Machanic
    SQL Server MVP
Page 1 of 1 (7 items)