Try to tackle the problem in this way:
-
Does the problem occur on the command-line? If an SQL command presented on the command line does not do what it is supposed to do, then you have an SQL server problem... highly unlikely.
-
My best guess is that the statement is sometimes throwing an error (or that some SQL trigger or integrity check is getting involved), and your code isn’t checking for the problem correctly.
Watch out for do or die, because this checks to see if do is returning a True result. I usually use defined($DBI::err) although there might be a better way.