SQL Server Central is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
Search:  
 
 

Ansi Options Part 3 - ANSI_NULL_DFLT_ON

By Steve Jones, 2002/08/07

Total article views: 3512 | Views in the last 30 days: 13

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

By Steve Jones, 2002/08/07

Total article views: 3512 | Views in the last 30 days: 13
Your response
 
 
Related tags

Basic Querying     SQL Server 7, 2000    
Configuring     T-SQL    
 
Already registered?  

Free registration required

To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.

Register

E-mail address:
Password:
Password (confirm):

  

Subscriptions

We ask you to register on the site and subscribe to our newsletters. Subscribing to our newsletters gets you:

  • ALL of our content (thousands of articles, scripts, and forum postings)
  • A daily newsletter (example)
  • A weekly news round up (example)
  • The opportunity to ask and answer questions in our forums
  • A daily Question of the Day to test and help you increase your knowledge of SQL Server.

We ask that you give the newsletter a try for a week. Over 200,000 SQL Server Professionals a day find it entertaining and useful. If not, you are welcome to unsubscribe at anytime.

Steve Jones
Editor, SQLServerCentral.com