【问题标题】:customize an error message in SQL server 2008 R2 on win 7在 win 7 上自定义 SQL server 2008 R2 中的错误消息
【发布时间】:2014-08-16 08:03:15
【问题描述】:

我需要在win 7的SQL server 2008 R2(不支持throw)中自定义错误信息。

  BEGIN TRY
   DECLARE @result INT
   SET @result = LOG(-1)
  END TRY

 BEGIN CATCH
  EXEC sys.sp_addmessage 60000, 16, 'log() argument is not positive'
  RAISERROR (60000, 16, 1)
 END CATCH

但是,结果总是一样的:

An invalid floating point operation occurred.

谢谢

【问题讨论】:

  • 这是一个解析时间错误,因此在代码尝试执行之前捕获了 -1。您可以通过将 try 中的内容作为动态 SQL 执行来绕过解析。

标签: sql sql-server sql-server-2008 exception windows-7


【解决方案1】:

您可以通过为 Log 参数使用变量来解决此问题:

DECLARE @result INT
DECLARE @insidelog INT = -1
IF @insidelog < 0
    BEGIN
        EXEC sys.sp_addmessage 60000, 16, 'log() argument is not positive'
        RAISERROR (60000, 16, 1)
    END
ELSE
    BEGIN
        SET @result = LOG(@insidelog)
    END

我假设您无论如何都会将该数字作为变量传递。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    相关资源
    最近更新 更多