In SQL Server, what does “SET ANSI_NULLS ON” mean?

前端 未结 7 2260
执念已碎
执念已碎 2020-11-27 13:22

The definition says:

When SET ANSI_NULLS is ON, a SELECT statement that uses WHERE column_name = NULL returns zero rows even if there are null values

7条回答
  •  情话喂你
    2020-11-27 13:56

    https://docs.microsoft.com/en-us/sql/t-sql/statements/set-ansi-nulls-transact-sql

    When SET ANSI_NULLS is ON, a SELECT statement that uses WHERE column_name = NULL returns zero rows even if there are null values in column_name. A SELECT statement that uses WHERE column_name <> NULL returns zero rows even if there are nonnull values in column_name.

    For e.g

    DECLARE @TempVariable VARCHAR(10)
    SET @TempVariable = NULL
    
    SET ANSI_NULLS ON
    SELECT 'NO ROWS IF SET ANSI_NULLS ON' where    @TempVariable = NULL
    -- IF ANSI_NULLS ON , RETURNS ZERO ROWS
    
    
    SET ANSI_NULLS OFF
    SELECT 'THERE WILL BE A ROW IF ANSI_NULLS OFF' where    @TempVariable =NULL
    -- IF ANSI_NULLS OFF , THERE WILL BE ROW !
    

提交回复
热议问题