My application is inserting duplicate records and I'm trying to figure out what causes that. It could be coming from the UI application, some stored proc or a nightly SQL job. I have looked into everything I can find and can't figure it out.
The table in question can have multiple userId but only one record can be active=1 for a user. So everytime I insert into the table, I update the current record with active=1 to 0 before inserting a new record with active=1.
How can I check where the duplicate values with multiple records of active=1 is happening for a userId?
1条答案
按热度按时间mm5n2pyu1#
The table in question can have multiple userId but only one record can be active=1 for a user
So put a unique index on userid filtered to active=1,eg
The second insert fails with
Then you'll both prevent the duplicates from occurring and know who is attempting to insert duplicates because they'll get errors.