Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What causes SQL Server to return the message 'The statement has been terminated'?

I have a very simple INSERT statement being executed from a PHP script running on a Linux Apache web server. I can run the query fine from within SQL Management Studio and it normally runs fine from PHP as well. However, every once in awhile I get an error message from my PHP script that the query failed and the mssql_get_last_message() function returns 'The statement has been terminated'.

What sources can cause this message to be returned from SQL Server?

like image 491
Wally Lawless Avatar asked Jun 16 '10 13:06

Wally Lawless


People also ask

What is the use of error message in SQL?

ERROR_MESSAGE (Transact-SQL) This function returns the message text of the error that caused the CATCH block of a TRY...CATCH construct to execute. Syntax. Return Types. Return Value. When called in a CATCH block, ERROR_MESSAGE returns the complete text of the error message that caused the CATCH block to run.

Why do I get a T-SQL error message when running a procedure?

You get this message when you execute T-SQL statements or stored procedures that they affect rows. To this end, SQL Server returns the number of records which were affected by the database operation you performed.

What happens when insert fails in SQL?

INSERT fails. The statement has been terminated. As the message suggests, you are trying to insert a new record into a table and one of the columns is being assigned a NULL value but the column does not allow NULLs. To illustrate, let’s say you have the following table definitions:

What happens if a query does not return any data?

If the query doesn't return any data within the configured time-out value (typically 30 seconds), the application cancels the query and generates one of these error messages: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.


1 Answers

You have found one of the most annoying parts of SQL Server. There are situations where an error can be raised, and SQL will generated two error messages: the first to explain what the error was, and the second to say something useful like "The statement has been terminated" (which, technically, is error number 3621). The thing is that SQL, and most everything else that touches it--such as PHP--can only see/pick up/process/or otherwise utilize that last unlcear error message. The one that's actually useful gets lost.

The quick way to figure out what's going on is to run the sequence of commands leading up to the error from SSMS. This, apparently, will not work for you.

A fussier way to figure it out is to fire up SQL Profiler to track the Exception event, and then run your process. This should show all errors that occured. Tossing in relevant other events (SP:Starting, SP:StmtStarting, SQL:BatchStarting, whatever is applicable to the code your submitting to the database) will show which command is raising the error.

like image 200
Philip Kelley Avatar answered Nov 15 '22 00:11

Philip Kelley