I couldn't guess what's going on without seeing all the scripts, but one possibility is that in some scripts the $sth goes out of scope before the call to $dbh->disconnect. Another possibility is that the data is in a different state when you perform the queries. BTW, I believe what you are seeing is a warning, not an error. I surmise this because you aren't using RaiseError. I'd recommend using RaiseError on general principles. If you really want to track down exactly why you get the warning, you might google for the text of the warning and the name of your DBMS, e.g. -- ODBC "disconnect invalidates"