Skip to main content

Posts

Showing posts from September, 2007

Why ' set ANSI_NULL ON ' while writing SP

-- Create table t1 and insert values. CREATE TABLE t1 (a INT NULL) INSERT INTO t1 values (NULL) INSERT INTO t1 values (0) INSERT INTO t1 values (1) GO When SET ANSI_NULLS is ON, all comparisons against a null value evaluate to UNKNOWN.  -- SET ANSI_NULLS to ON and test. SET ANSI_NULLS ON GO DECLARE @varname int =  NULL SELECT * FROM t1  WHERE a = @varname  -- No output SELECT * FROM t1  WHERE a <> @varname -- No output SELECT * FROM t1  WHERE a IS NULL     -- NULL  GO When SET ANSI_NULLS is OFF, comparisons of all data against a null value evaluate to TRUE if the data value is NULL -- SET ANSI_NULLS to OFF and test. SET ANSI_NULLS OFF  GO DECLARE @varname int =  NULL SELECT * FROM t1  WHERE a = @varname  -- NULL SELECT * FROM t1  WHERE a <> @varname -- 0 , 1 SELECT * FROM t1  WHERE a IS NULL     -- NULL  GO -- Drop table t1. DROP TABLE t1