ISNUll problem. Where clause doesn't work properly when I use isnull clause

Hey. I need to substitute a value from a table if the input var is null. This is fine if the value coming from table is not null. But, it the table value is also null, it doesn't work. The problem I'm getting is in the isnull line which is in Dark green color because @inFileVersion is set to null explicitly and when the isnull function evaluates, value returned from DR.FileVersion is also null which is correct. I want the null=null to return true which is why i set ansi_nulls off. But it doesn't return anything. And the select statement should return something but in my case it returns null. If I comment the isnull statements in the where clause, everything works fine. Please tell me what am I doing wrong. Is it possible to do this without setting the ansi_nulls to off? Thank you

setansi_nullsoff

go

declare

@inFileName VARCHAR(100),

@inFileSizeINT,

@Idint,

@inlanguageidINT,

@inFileVersion VARCHAR(100),

@ExeStateint

set @inFileName='A0006337.EXE'

set @inFileSize= 28796

set @Id= 1

set @inlanguageid=null

set @inFileVersion=NULL

set @ExeState=0

selectDr.StateIDfromtable1 dR

where

DR.[FileName]= @inFileName

AND DR.FileSize=@inFileSize

AND DR.FileVersion = isnull(@inFileVersion,DR.FileVersion)

AND DR.languageid=isnull(@inlanguageid,null)

AND DR.[ID]= @ID

)

go

setansi_nullson

[3876 byte] By [Tej] at [2007-12-24]
# 1

well actually you dont need to change the setting

if you're up to something like this

AND isnull (DR.FileVersion,-1) = isnull(@inFileVersion,-1)

joeydj at 2007-8-31 > top of Msdn Tech,SQL Server,Transact-SQL...
# 2

There is a slight problem with this. If the right side is null, it will evaluate to -1. If the left side is not null, it will evaluate to value stored in the table. It's VERY likely that the value in the table won't be -1. So the condition will be false. But, in actuality, it should be true, correct? Shouldn't it be like this?

AND isnull (DR.FileVersion,-1) = isnull(@inFileVersion,isnull(DR.FileVersion,-1))

Thank you

Tej at 2007-8-31 > top of Msdn Tech,SQL Server,Transact-SQL...
# 3

with this

AND isnull (DR.FileVersion,-1) = isnull(@inFileVersion,-1)

the ending equation would be

and (-1 = -1) which evaluates to true.

meaning null=null

remember that this equation resides in the "where clause" and not on the

select clause. if you want to have it returned you must

place a "case clause" in the select statement to evaluate this

nevertheless this clause must still exist in the where clause

to include the nulls

joeydj at 2007-8-31 > top of Msdn Tech,SQL Server,Transact-SQL...

SQL Server

Site Classified