I’m having a problem with a stored procedure. It’s comparing a new record with an existing one to check for changes. This is done like this:
SELECT 1 FROM Table
WHERE Id= @Id
AND Field1 = @Field1
AND Field2 = @Field2
AND Field3 = @Field3
AND Field4 = @Field4
AND Field5 = @Field5
AND LEDTS IS NULL
This seems to fail when fields are NULL. I know = NULL doesn’t work the way you might expect it to, but I didn’t know it also fails in things like this. When I change the field comparison to things like
(Field1 IS NULL AND Field2 IS NULL) OR Field1 = @Field2
it does work. Is there a better way to do this?
SET ANSI_NULLScan control this behavior… SettingSET ANSI_NULLS OFFwill makeNULL = NULLevaluate to true.But I would recommend against actually setting
SET ANSI_NULLS OFF, and instead try to limit the fields in the database that are “nullable”.