开发者

How to manage NULL values with numeric fields in cursor?

开发者 https://www.devze.com 2022-12-12 15:13 出处:网络
How t开发者_JS百科o manage NULL values in numeric fields returned by cursor in Select stament, to manage efficienly aritmetic operations ?

How t开发者_JS百科o manage NULL values in numeric fields returned by cursor in Select stament, to manage efficienly aritmetic operations ?


  1. Don't use cursors.

  2. If you must (really?), you can use the ISNULL function:

    SELECT ISNULL(fieldname, 0)
    

will give you a "0" (zero) instead of NULL.


ISNULL(value, replacement)

will replace value with replacement if value IS NULL

http://msdn.microsoft.com/en-us/library/ms184325.aspx


Assuming you cannot avoid a cursor in the first place, I don't understand why a NULL would be handled much differently in a variable than you would in a query - there is INSULL, COALESCE, CASE WHEN etc.

One interesting thing:

DECLARE @v as int -- initialized to NULL

{ -- loop through a cursor
FETCH NEXT INTO @v
}

You won't be able to necessarily distinguish an uninitialized @v from when the last row's @v setting was NULL.

0

精彩评论

暂无评论...
验证码 换一张
取 消