patternsqlMinor
SET ANSI_NULLS at the database level versus setting it at the table level
Viewed 0 times
theversuslevelsettabledatabasesettingansi_nulls
Problem
I have generated schema-only script for my sql server 2008 R2, here is part of it:-
```
USE [master]
GO
/ Object: Database [**] Script Date: 03/25/2015 12:17:09 ****/
CREATE DATABASE [****] ON PRIMARY
( NAME = N'TMS', FILENAME = N'D:\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\****.mdf' , SIZE = 11264KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
LOG ON
( NAME = N'TMS_log', FILENAME = N'L:\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data\****.ldf' , SIZE = 4672KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO
ALTER DATABASE [****] SET COMPATIBILITY_LEVEL = 100
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [****].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [****] SET ANSI_NULL_DEFAULT OFF
GO
ALTER DATABASE [****] SET ANSI_NULLS OFF
GO
ALTER DATABASE [****] SET ANSI_PADDING OFF
GO
ALTER DATABASE [****] SET ANSI_WARNINGS OFF
GO
ALTER DATABASE [****] SET ARITHABORT OFF
GO
ALTER DATABASE [****] SET AUTO_CLOSE OFF
GO
ALTER DATABASE [****] SET AUTO_CREATE_STATISTICS ON
GO
ALTER DATABASE [****] SET AUTO_SHRINK OFF
GO
ALTER DATABASE [****] SET AUTO_UPDATE_STATISTICS ON
GO
ALTER DATABASE [****] SET CURSOR_CLOSE_ON_COMMIT OFF
GO
ALTER DATABASE [****] SET CURSOR_DEFAULT GLOBAL
GO
ALTER DATABASE [****] SET CONCAT_NULL_YIELDS_NULL OFF
GO
ALTER DATABASE [****] SET NUMERIC_ROUNDABORT OFF
GO
ALTER DATABASE [****] SET QUOTED_IDENTIFIER OFF
GO
ALTER DATABASE [****] SET RECURSIVE_TRIGGERS OFF
GO
ALTER DATABASE [****] SET DISABLE_BROKER
GO
ALTER DATABASE [****] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
GO
ALTER DATABASE [****] SET DATE_CORRELATION_OPTIMIZATION OFF
GO
ALTER DATABASE [****] SET TRUSTWORTHY OFF
GO
ALTER DATABASE [****] SET ALLOW_SNAPSHOT_ISOLATION OFF
GO
ALTER DATABASE [****] SET PARAMETERIZATION SIMPLE
GO
ALTER DATABASE [****] SET READ_COMMITTED_SNAPSHOT OFF
GO
ALTER DATABASE [****] SET HONOR_BROKER_PRIORITY OFF
GO
ALTER DATABASE [****] SET READ_WRITE
GO
ALTER DATABASE [****] SET RECOVERY FULL
```
USE [master]
GO
/ Object: Database [**] Script Date: 03/25/2015 12:17:09 ****/
CREATE DATABASE [****] ON PRIMARY
( NAME = N'TMS', FILENAME = N'D:\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\****.mdf' , SIZE = 11264KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
LOG ON
( NAME = N'TMS_log', FILENAME = N'L:\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data\****.ldf' , SIZE = 4672KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO
ALTER DATABASE [****] SET COMPATIBILITY_LEVEL = 100
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [****].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [****] SET ANSI_NULL_DEFAULT OFF
GO
ALTER DATABASE [****] SET ANSI_NULLS OFF
GO
ALTER DATABASE [****] SET ANSI_PADDING OFF
GO
ALTER DATABASE [****] SET ANSI_WARNINGS OFF
GO
ALTER DATABASE [****] SET ARITHABORT OFF
GO
ALTER DATABASE [****] SET AUTO_CLOSE OFF
GO
ALTER DATABASE [****] SET AUTO_CREATE_STATISTICS ON
GO
ALTER DATABASE [****] SET AUTO_SHRINK OFF
GO
ALTER DATABASE [****] SET AUTO_UPDATE_STATISTICS ON
GO
ALTER DATABASE [****] SET CURSOR_CLOSE_ON_COMMIT OFF
GO
ALTER DATABASE [****] SET CURSOR_DEFAULT GLOBAL
GO
ALTER DATABASE [****] SET CONCAT_NULL_YIELDS_NULL OFF
GO
ALTER DATABASE [****] SET NUMERIC_ROUNDABORT OFF
GO
ALTER DATABASE [****] SET QUOTED_IDENTIFIER OFF
GO
ALTER DATABASE [****] SET RECURSIVE_TRIGGERS OFF
GO
ALTER DATABASE [****] SET DISABLE_BROKER
GO
ALTER DATABASE [****] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
GO
ALTER DATABASE [****] SET DATE_CORRELATION_OPTIMIZATION OFF
GO
ALTER DATABASE [****] SET TRUSTWORTHY OFF
GO
ALTER DATABASE [****] SET ALLOW_SNAPSHOT_ISOLATION OFF
GO
ALTER DATABASE [****] SET PARAMETERIZATION SIMPLE
GO
ALTER DATABASE [****] SET READ_COMMITTED_SNAPSHOT OFF
GO
ALTER DATABASE [****] SET HONOR_BROKER_PRIORITY OFF
GO
ALTER DATABASE [****] SET READ_WRITE
GO
ALTER DATABASE [****] SET RECOVERY FULL
Solution
ANSI_NULL_DEFAULT { ON | OFF } :
When you dont specify
When SET to
Connection-level settings that are set by using the SET statement override the default database-level setting for ANSI_NULL_DEFAULT. By default, ODBC and OLE DB clients issue a connection-level SET statement setting ANSI_NULL_DEFAULT to ON for the session when connecting to an instance of SQL Server.
ANSI_NULLS { ON | OFF }:
When
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.
When
When SET ANSI_NULLS is OFF, the Equals (=) and Not Equal To (<>) comparison operators do not follow the ISO standard. A SELECT statement that uses WHERE column_name = NULL returns the rows that have null values in column_name. A SELECT statement that uses WHERE column_name <> NULL returns the rows that have nonnull values in the column. Also, a SELECT statement that uses WHERE column_name <> XYZ_value returns all rows that are not XYZ_value and that are not NULL.
Always remember that session level setting will override database level setting for the above SET options.
Reference : SET ANSI_NULL_DFLT_ON and SET ANSI_NULLS
When you dont specify
ANSI_NULL_DEFAULT in CREATE TABLE or ALTER TABLE statements, this database option will determine the default value, NULL or NOT NULL for a column or CLR user-defined type.When SET to
ON, the DEFAULT value is NULL. When SET to OFF, the DEFAULT value is NOT NULL.Connection-level settings that are set by using the SET statement override the default database-level setting for ANSI_NULL_DEFAULT. By default, ODBC and OLE DB clients issue a connection-level SET statement setting ANSI_NULL_DEFAULT to ON for the session when connecting to an instance of SQL Server.
ANSI_NULLS { ON | OFF }:
ANSI_NULL should be ON. This will be a default behavior in future versions of SQL Server.When
ANSI_NULLS is set to ON then NULLs follow the ISO compliant behavior of the Equals (=) and Not Equal To (<>) comparison operators. 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.
When
ANSI_NULLS is set to OFF then a comparison of NULL=NULL or NULL<>NULL returns TRUE or FALSE respectively, instead of an unknown answer NULL. It treats NULL into a value of its own.When SET ANSI_NULLS is OFF, the Equals (=) and Not Equal To (<>) comparison operators do not follow the ISO standard. A SELECT statement that uses WHERE column_name = NULL returns the rows that have null values in column_name. A SELECT statement that uses WHERE column_name <> NULL returns the rows that have nonnull values in the column. Also, a SELECT statement that uses WHERE column_name <> XYZ_value returns all rows that are not XYZ_value and that are not NULL.
Always remember that session level setting will override database level setting for the above SET options.
Reference : SET ANSI_NULL_DFLT_ON and SET ANSI_NULLS
Context
StackExchange Database Administrators Q#106046, answer score: 5
Revisions (0)
No revisions yet.