ERROR_MESSAGE () is a very powerful function in SQL2005. It can obtain error messages due to operation exceptions and return the messages to the client, to help users and developers understand what the program has taken.
However, when I recently used it, I found that it was not true.
The following statement:
1 BEGIN TRY
2 select sum (T0.Quantity) FROM INV1 T0
3 WHERE T0.DocEntry = (SELECT T1.DocEntry from oinv T1
4 WHERE T1.U _ HDBH = '000000 ')
5END TRY
6 BEGIN CATCH
7 DECLARE @ errMsg as VARCHAR (4000)
8 SET @ errMsg = (SELECT ERROR_MESSAGE ())
9 SELECT @ errMsg
10END CATCH
11GO
When using this statement, I can return the following message:
More than one value is returned by the subquery. When the subquery follows in = ,! =, <, <=,>,> =, Or use a subquery as an expression.
This error is correct because multiple values are returned after my query "T0.DocEntry". If I add a TOP 1 value before the SELECT statement, this error can be avoided. But that is not logical. I want to return this error message to the client. The error here is normal, but when I move this statement to the cursor, the error message becomes:
The cursor named 'xx' already exists.
Why?