Duplicate Record Inserts

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default Duplicate Record Inserts

    The problem is that records are not being inserted only once. The included code
    is processed in a page after a form. The following 3 events randomly occur:
    1. record inserted once
    2. record inserted twice
    3. error after record is inserted with a duplicate primary key (empID)

    JS alerts before and after each query pop-up only once, and html messages are
    written once. The page seems to be processed once, but the queries are randomly
    processed. cflock around each query didn't fix it. Has anyone run into this
    problem with SQL Server 2000 and how did you solve it?

    Thanks for your time,
    Ren



    <cfquery name="getNextTempEmpID" datasource="#request.dataSource#">
    SELECT MIN(EMPLOYEE_ID) AS MinID
    FROM TBL_ALL_EMP
    </cfquery>

    <cfset empID=getNextTempEmpID.MinID - 1>

    <cfquery name="addTempEmp" datasource="#request.dataSource#">
    INSERT INTO TBL_ALL_EMP (EMPLOYEE_ID,
    EMP_FRST_NM,
    EMP_LST_NM)
    VALUES ('#Trim(empID)#',
    '#Trim(FirstName)#',
    '#Trim(LastName)#')
    </cfquery>

    Ren0420 Guest

  2. Similar Questions and Discussions

    1. Avoid Duplicate Inserts
      Hi All, I?m trying to do a normal INSERT in to a SQL server database. I take the values from a form and pass it on to another page and calling a...
    2. Duplicate record insertion
      Hi there, I have a DW insert record behaviour on a php page. If the user clicks Submit quickly multiple times, I end up with duplicate records. Any...
    3. MS Access SQL: duplicate record... ?SELECT INTO....?
      What syntax would you use to dupe a record? Thanks! -- Scotter
    4. Making duplicate record created appear in tab order?
      I made the duplicate record so that I could get around the automatic pulldown of a menu when it's tabbed into. Boy, does it ever work beautifully!...
    5. Duplicate a record in a table from a Form.
      I am trying to duplicate a record within the same table. Basically I have fields that will always be the same. Field 2 and 3 will always equal...
  3. #2

    Default Re: Duplicate Record Inserts

    You can have a race condition where two users, at the "same instant", could
    both grab the same value for MinID, both decrement it and only the first one is
    inserted correctly. The second one will throw an error since the empID is
    already set in the database. Use CFTRANSACTION to resolve the problem. See
    [url]http://livedocs.macromedia.com/coldfusion/5.0/CFML_Reference/Tags103.htm#1104164[/url]
    for more information.

    jdeline Guest

  4. #3

    Default Re: Duplicate Record Inserts

    Originally posted by: Ren0420
    The problem is that records are not being inserted only once. The included
    code is processed in a page after a form. The following 3 events randomly occur:
    1. record inserted once
    2. record inserted twice
    3. error after record is inserted with a duplicate primary key (empID)

    JS alerts before and after each query pop-up only once, and html messages are
    written once. The page seems to be processed once, but the queries are randomly
    processed. cflock around each query didn't fix it. Has anyone run into this
    problem with SQL Server 2000 and how did you solve it?

    Thanks for your time,
    Ren



    You are doing some very unusual things. Topping the list is subtracting one
    from the min rather than adding one to the max. You also appear to be using a
    numeric string as your primary key. Why don't you just use a number?

    I don't do very much work with sql server, but doesn't it have a way to just
    generate the next number for you?

    Dan Bracuk Guest

  5. #4

    Default Re: Duplicate Record Inserts

    Protect your data by design and by transaction.

    First, go into Enterprise manager or Query analyzer and put an index on
    EMPLOYEE_ID requiring unique values. You may have to remove duplicate or
    corrupt rows to do this but you don't want such rows anyway.

    Next, put another unique index on the combination of columns: FirstName and
    LastName (and also middle name if used).
    Again, removing bad data as needed.

    Then, make one of these indices clustered. Which one depends on what you
    search on the most (probably ID).

    Now, your DB will reject bad or duplicate data by design and Coldfusion
    mistakes will thus be caught quicker.

    Finally, the suggestion to use <cftransaction> was a good one. But for SQL
    server there is a better way. (Faster too).

    Use SQL server's transaction handling like in the attached code:



    <CFQUERY name="addTempEmp" datasource="#request.dataSource#">
    BEGIN TRAN sqlTranEmpInsert

    INSERT INTO
    TBL_ALL_EMP
    (
    EMPLOYEE_ID
    , EMP_FRST_NM,
    , EMP_LST_NM
    )
    SELECT
    (
    SELECT MIN (EMPLOYEE_ID) - 1 <!--- If EMPLOYEE_ID is not
    numeric, you will need Cast() here. --->
    FROM TBL_ALL_EMP
    )
    , '#Trim (FirstName)#'
    , '#Trim (LastName)#'

    COMMIT TRAN sqlTranEmpInsert
    </CFQUERY>

    MikerRoo Guest

  6. #5

    Default Re: Duplicate Record Inserts

    datasource="#request.dataSource#"
    Can well give rise to spooky effects. The application scope is more appropriate.


    BKBK Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139