Hi everyone, having a really weird issue that so far 4 of our developers have looked at, and none of us can figure out. I've done some research online but didn't find anything, so I'm hoping there's a SQL guru here who might have an idea.
There is a 10 year old stored proc at my work that supports a legacy application by syncing some aggregated data from one database to another via a linked server. For 10 years it has worked without issue, but about a month ago we started to see some strange, unexplained behaviour.
THE PROBLEM: The stored proc includes an INSERT statement with a WHERE NOT EXISTS condition, to avoid inserting any duplicates, but we're seeing duplicates being inserted occasionally.
It doesn't occur every day, and when it does occur it only affects a tiny handful of records (maybe only 10 records out of 300-400). Examining the data reveals a pattern that from the first moment that one of these affected records gets inserted, it then also inserts exactly one duplicate each hour after that (the SQL agent job runs hourly), without ever missing an hour. It continues doing this until some arbitrary point in the afternoon/evening when it just suddenly stops happening for all affected records at the same time, with no involvement from us at all.
But the strangest part is that while the issue is still happening I can run the same SQL statements in my own user session, directly in the prod environment, with the exact same data, and it will actually produce the correct outcome. I can repeat my test as many times as I want and it will never insert a dupe, but then sure enough on the next run of the sync job another dupe magically appears.
Link to the SQL is provided below. It's anonymised a little bit, but nothing of importance was changed.
And before you ask, yeah the "READUNCOMMITTED" hints are not great. This sort of thing was used extensively all over the place before I started working here, our boss was adamant about using "READUNCOMMITTED" or "NOLOCK" hints in any non-critical queries to avoid shared locks. I tried to convince him a few times that it's a bad idea but he wouldn't have it.
https://pastebin.com/XMPHFF3W
Some other things I've confirmed during my troubleshooting:
Any ideas?