SQLServerCentral Article

Ansi Options Part 3 - ANSI_NULL_DFLT_ON

,

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

Rate

5 (1)

You rated this post out of 5. Change rating

Share

Share

Rate

5 (1)

You rated this post out of 5. Change rating