Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find out which line of which stored procedure an error occurred on?

When I try to run a particular stored procedure on my MS SQL 2005 database, I get an error like the following:

Subquery returned more than 1 value. This is not permitted when
the subquery follows =, !=, <, <= , >, >= or when the subquery
is used as an expression

The SP in query is very long and calls other SPs. This error is obviously being produced by SQL itself, and returned all the way up the call stack, but without mentioning which SP or line number caused the problem. How can I find out from where the error was thrown so I can debug it more easily?

like image 353
apenwarr Avatar asked Oct 29 '08 20:10

apenwarr


People also ask

How do you find a line of error in SQL?

@@ERROR returns an error number in the statement immediately following the one that causes an error, or in the first statement of a CATCH block. In nested CATCH blocks, ERROR_LINE returns the error line number specific to the scope of the CATCH block in which it is referenced.

How do you check where a stored procedure is called?

Expand Databases, expand the database in which the procedure belongs, and then expand Programmability. Expand Stored Procedures, right-click the procedure and then click View Dependencies. View the list of objects that depend on the procedure. View the list of objects on which the procedure depends.


1 Answers

Using the Try/Catch block should give you what you are looking for.

In the scope of a CATCH block, the following system functions can be used to obtain information about the error that caused the CATCH block to be executed:

* ERROR_NUMBER() returns the number of the error.
* ERROR_SEVERITY() returns the severity.
* ERROR_STATE() returns the error state number.
* ERROR_PROCEDURE() returns the name of the stored procedure or trigger where the error occurred.
* ERROR_LINE() returns the line number inside the routine that caused the error.
* ERROR_MESSAGE() returns the complete text of the error message. The text includes the values supplied for any substitutable parameters, such as lengths, object names, or times.

So, in your case, ERROR_LINE() and ERROR_PROCEDURE() should be what you want...

like image 97
Kevin Fairchild Avatar answered Oct 25 '22 17:10

Kevin Fairchild