[英]catch an exception for argument of log() that may be a NaN or NULL or negative in SQl server 2008 R2
在SQl服務器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時如何捕獲異常?
謝謝
與上一篇文章的答案相比,這是一個很小的變化:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.