5

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
GO
ALTER DATABASE [****] SET  MULTI_USER
GO
ALTER DATABASE [****] SET PAGE_VERIFY CHECKSUM
GO
ALTER DATABASE [****] SET DB_CHAINING OFF
GO
USE [****]
GO
/****** Object:  User [AD-ITSERVICES\TMSDB.user]    Script Date: 03/25/2015 12:17:09 ******/
CREATE USER [AD-ITSERVICES\test.user] FOR LOGIN [****\test.user] WITH DEFAULT_SCHEMA=[dbo]
GO
/****** Object:  Table [dbo].[TechnologyTypes]    Script Date: 03/25/2015 12:17:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[TechnologyTypes](
    [AssetTypeID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](50) NOT NULL,
    [IncludedInSearch] [bit] NULL,
 CONSTRAINT [PK_AssetType] PRIMARY KEY CLUSTERED 
(
    [AssetTypeID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY],
 CONSTRAINT [IX_AssetType] UNIQUE NONCLUSTERED 
(
    [Name] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[TechnologyStatus]    Script Date: 03/25/2015 12:17:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON

now i am confused on why i go this sentence :-

ALTER DATABASE [****] SET ANSI_NULL_DEFAULT OFF
GO
ALTER DATABASE [****] SET ANSI_NULLS OFF

but when creating any sql table i will get :-

SET ANSI_NULLS ON

so what is the final result; is ANSI_NULLS will be equal to off or on ? from my understanding the "SET ANSI_NULLS" will be OFF at the database level, unless i set it ON at the table level ? is this correct ? any why i got the ANSI_NULLs set to off at the database level ?

4

ANSI_NULL_DEFAULT { ON | OFF } :

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

  • This part may be confusing for readers: When you dont specify ANSI_NULL_DEFAULT in CREATE TABLE or ALTER TABLE statements… You probably meant to say something like When you don't specify the nullability of a column in CREATE TABLE or ALTER TABLE statements… – Andriy M Jul 7 '15 at 7:43
  • And you haven't addressed the Why bit (why i got the ANSI_NULLs set to off at the database level). – Andriy M Jul 7 '15 at 7:45
  • For "why i got the ANSI_NULLs set to off at the database level" see my answer dba.stackexchange.com/a/185844/134546 – rmcsharry Sep 22 '18 at 20:07
2

From a comment: 'And you haven't addressed the Why bit (why i got the ANSI_NULLs set to off at the database level)'

At the database level, when you generate a database script it will take the settings from whatever they are set to on the database (you can see what they are for any databsae by right clicking on it, select Properties and then Options).

By default SQL Server, when installed, sets these settings to OFF in the model database. Since all new databases are created using model as the base, new databases will also have this setting (and others, eg ANSI PADDING) set to OFF/False unless you explicitly write your own SET statements to override the model defaults when creating a new database.

More info here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.