Home Forums Programming General Duplicate Records Being Inserted RE: Duplicate Records Being Inserted

  • Others gave you the reason why this occurs, but the way to avoid this situation would be to key off another field within the table and check against sometype of timestamp datetime field and a customer id type to see if a particular customer has within a relativly short timespan (e.g. within a few seconds maybe) already made an entry.

    Still not a guarntee but will eliminate the problem a majority of the time. Eg. keying off a customer_id

    CREATE TABLE some_table(

    customer_id int NOT NULL,

    added datetime NOT NULL)

    IF NOT EXISTS(SELECT TOP 1 * FROM some_table WHERE customer_id = @customer_ID AND added > DATEADD(s,-1,GETDATE()))

    INSERT INTO some_table VALUES (@customer_id, GETDATE())