如何使用 SQL Server 显示 raiserror() 生成的错误消息

发布于 2024-07-21 15:08:30 字数 499 浏览 3 评论 0原文

我正在通过 JDBC 从 jsp 与 SQL Server 2005 进行交互(这是一个作业,而不是一个真正的项目),并且我在我正在使用的数据库中创建了一个触发器。 如果执行此触发器时未满足某个条件,我会通过 raiserror() 引发错误。 我希望此错误显示在通过 JDBC 调用 SQL Server 查询的实际页面上,但目前,当我打印 SQLException.getMessage() 的结果时,我只是收到以下默认消息:

The transaction returned in the trigger 。 该批次已中止。

有谁知道如何提取我在触发代码中实际传递给 raiserror 的文本? 我已经尝试过:

  • SQLException.getState()
  • SQLException.getNextException()
  • SQLException.getCause()
  • SQLException.toString()

I am interacting with SQL Server 2005 from a jsp via JDBC (this is an assignment not a real project) and I have created a trigger in the database I am using. If a certain condition is not met when this trigger executes I raise an error via raiserror(). I would like this error to be displayed on the actual page that calls the SQL Server query via JDBC, but currently I am just getting the following default message when I print out the result of SQLException.getMessage():

The transaction ended in the trigger. The batch has been aborted.

Does anyone know how to extract the text I actually passed to raiserror in my trigger code? I have already tried:

  • SQLException.getState()
  • SQLException.getNextException()
  • SQLException.getCause()
  • SQLException.toString()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

-小熊_ 2024-07-28 15:08:30

我想到了。 该问题与我在调用 raiserror() 时选择的严重性有关。 我为此参数传递了 10,因为我看到过一个将 10 放在那里的示例,但为了将错误文本传递给 SQLException,严重性必须为 11 或更高。 更改严重性后,一切正常。 如果其他人有类似的问题,我建议尝试这个。 另外,谷歌 JDBC 错误处理以获取有用的文档(我会提交一个链接,但不允许新用户)。

I figured it out. The problem was related to the severity I was choosing when calling raiserror(). I was passing in 10 for this argument as I had seen an example that put 10 there, but in order for the error text to get passed to the SQLException the severity must be 11 or higher. After changing the severity it all works fine. If anyone else has a similar problem I would recommend trying this. Also google JDBC error handling for useful documentation (I would submit a link, but new users aren't allowed).

向地狱狂奔 2024-07-28 15:08:30

您需要迭代 getNextException() 返回的所有异常

You need to iterate all exceptions returned with getNextException()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文