Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Ansi Options Part 3 - ANSI_NULL_DFLT_ON

By Steve Jones,

ANSI Options Part 3 - ANSI_NULL_DFLT_ON

This article continues my series on ANSI settings in SQL Server. The other parts are:

Introduction

SQL Server conforms to a number of ANSI standards to varying degrees. The level of compliance or even whether a particular object behaves as per an ANSI Standard is usually governed by an option setting. In this series I plan to look at the various options that are available in SQL Server

What is this option?

There are actually two options here. The ANSI_NULL_DFLT_ON and ANSI_NULL_DFLT_OFF, only one of which can be set to ON at a time. They can both be OFF, but if you set the second one to ON, then the first is automatically reset to OFF.

Confusing?

Well it was to me to read this, but it started to make sense as I investigated what these options actually do.

These settings only affect you if you issue a CREATE TABLE or ALTER TABLE statement. If so, then they are used to determine the nullability (whether NULLs are allowed) for columns for which you DO NOT specify a setting.

Another mouthful.

Maybe it's simpler to explain with an example. Suppose that I set the _ON option to ON.

SET ANSI_NULL_DFLT_ON ON 

Create table MyTest
( MyID int
  , MyChar char(1)
)
go
insert MyTest select 1, NULL

select * from MyTest
go
drop table MyTest
go
When I run this, I get the following:
MyID     MyChar
-----    --------
1        NULL
However, suppose I set the other option on. The _OFF option to ON.
SET ANSI_NULL_DFLT_OFF ON 

Create table MyTest
( MyID int
  , MyChar char(1)
)
go
insert MyTest select 1, NULL

select * from MyTest
go
drop table MyTest
go
When I run this, I get the following:
Server: Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'MyChar', table 'stevejonestest.dbo.MyTest'; column does not allow nulls. INSERT fails.
The statement has been terminated.

(0 row(s) affected)
Note that NULL is not an allowable value for an insert.

Now, if I specify the Nullability of the column in the CREATE TABLE statement, then this setting has no affect.

What to do?

The default setting is the _ON option set to ON for the SQL Server drivers. Leave it alone. The default setting for most applications is ON and the default server setting is for this to be ON, so I highly recommend you leave it on.

However, you should also not leave things to chance. Take a couple keystrokes and specify the nullability of your columns when you create your table. It only would have taken a few seconds for me to type:

Create table MyTest
( MyID int NOT NULL
  , MyChar char(1) NULL
)
go
and it would prevent any mistakes.

References:

  • Books Online - Search "Set ANSI_NULL_DFLT_ON"

Conclusions

Nothing earth shattering in this article. Indeed, most of this information can be found in Books Online. I would be interested to hear if anyone actually sets this to OFF and the reasoning.

As always I welcome feedback on this article using the "Your Opinion" button below. Please also rate this article.

Steve Jones
©dkRanch.net June 2002
Return to Steve Jones Home

Total article views: 3715 | Views in the last 30 days: 0
 
Related Articles
FORUM

Creating XML from SQL Server 2005 - What are my options?

Creating XML from SQL Server 2005 - What are my options?

FORUM

Options in tool box for creating packages in SQL Server 2005

Options in tool box for creating packages in SQL Server 2005

ARTICLE

Stairway to SQL Server Indexes: Level 12, Create Alter Drop

Options and impacts when creating, altering and dropping an index

FORUM

How does System table MSReplication_options created?

How does System table MSReplication_options get created?

FORUM

error when create new publication

Invalid column name 'options.' (Microsoft SQL Server, Error:207)

Tags
configuring    
sql server 7    
t-sql    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones