Why can't ERROR_MESSAGE () in SQL return the value I want?

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.