Home Forums SQL Server 7,2000 T-SQL IsNull() not behaving as expected; IsNull() vs. Coalesce() RE: IsNull() not behaving as expected; IsNull() vs. Coalesce()

  • I don't think that the problem is with ISNULL or COALESCE, but with the way the optimizer treats the data in the test table. At some point it decides that some values are constants:

    Try adding SET SHOWPLAN_ALL ON before running the query and you will get, for the Compute Scalar operation row, the following Argument value:

    DEFINE ([Expr1007]=If ([Expr1002]NULL) then [Expr1002] else 0+If ([Expr1006]NULL) then [Expr1006] else 0, [Expr1008]=isnull(10, 0)+isnull([Expr1006], 0), [Expr1009]=If ([Expr1002]NULL) then [Expr1002] else 0, [ConstExpr1022]=isnull(10, 0), [Expr1011]=If ([Expr1006]NULL) then [Expr1006] else 0, [Expr1012]=isnull([Expr1006], 0), [Expr1013]=If (If ([Expr1002]NULL) then [Expr1002] else 0isnull([Expr1002], 0)) then 'Error' else If (If ([Expr1006]NULL) then [Expr1006] else 0isnull([Expr1006], 0)) then 'Error' else '')

    You see that the optimizer decides that 10 is the value to use in the ISNULL function, not the value stored in the table.

    If you repeat the same in SQL2005 you never get into the constant.

    I don't understand why this happens, maybe someone with stronger skills may help diggind into this.

    Nice catch, anyway.

    Regards

    Gianluca

    Edited: code garbled by emoticons

    -- Gianluca Sartori