【问题标题】:catch an exception for argument of log() that may be a NaN or NULL or negative in SQl server 2008 R2在 SQL Server 2008 R2 中捕获 log() 参数的异常,该异常可能是 NaN 或 NULL 或负数
【发布时间】:2014-08-16 11:55:44
【问题描述】:

在 SQl server 2008 R2 中,我需要捕获 log() 参数的异常,它可能是 NaN 或 NULL 或负数。

BEGIN TRY

  DECLARE @ab float
  select @ab = log(c.ab) FROM t1 a inner join t2 c
    on t1.id = t2.id
                  -- @ab is selected from a table, but I do not know how to 
                  -- write the query for this case combined with capturing a possible exception.
 END TRY
 BEGIN CATCH 
  EXEC sys.sp_addmessage 60000, 16, 'log argument not positive'
  RAISERROR (60000, 16, 1)
 END CATCH

令我惊讶的是,log(null) 没有抛出异常!!!

我可以在这里使用“if”来抛出异常。
但是,@ab 将从表中选择,因此它来自列。 从表中选择@ab 时如何捕获异常?

谢谢

【问题讨论】:

  • 如果从表中选择了@ab,那么它将具有值或为空。您在 log 函数中所做的事情。发布该代码。

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


【解决方案1】:

这与您上一篇文章中的答案略有不同:

DECLARE @result INT
DECLARE @insidelog INT
SELECT @insidelog = field FROM tablename WHERE condition = 'met'
IF @insidelog < 0 OR @insidelog IS NULL
    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
    • 2016-08-24
    • 2015-11-14
    • 2017-02-20
    • 2012-10-06
    • 1970-01-01
    • 2017-12-27
    相关资源
    最近更新 更多