Solved

Default of null vs. not null in Temp tables

Posted on 2014-09-18
3
403 Views
Last Modified: 2014-09-19
Bumped into something very interesting this week. The SP below (version 1) works fine when executed from native SQL client like SQL Studio or ADO.NE . However, when executed from a PHP5 client (mssql_query($query,$this->dth_db);), it returns 'Cannot insert value null into column 'Something'. If I modify the SP to be like version 2 (null after Something column)

It appears that individual connections can change the way the temp table declarations handle null vs. not null when it is not explicitly specified in the table declaration. I did not even know this was possible much less know how to prevent it from happening again. I can make it a habit to explicitly declare null vs. not null for future work but I want to make sure existing code always default to null if it is not explicitly specified. Is this possible?



Version 1
alter proc TEST_spNullTest

as

create table #CallsToBill (
 Something int
)


insert into #CallsToBill select null

select * from #CallsToBill




Version 2
alter proc TEST_spNullTest

as

create table #CallsToBill (
 Something int null
)


insert into #CallsToBill select null

select * from #CallsToBill
0
Comment
Question by:dthansen
3 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 40331394
You can do a search on the stored procs:

SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%create table #%'
order by name

Open in new window


Just in your create tables specify the columns that can be null to null.

Note that if you do a select from an actual table - eg - select <columns> into #temp from table
they'll copy over whatever null properties you have.

sometimes I'll setup a table using
         select top 1 * into #temp from <table> ;
         delete from #temp;

and that way #temp holds the same template as a table.
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 40331623
>> but I want to make sure existing code always default to null if it is not explicitly specified. Is this possible? <<

I think the SET below will do it, but you must issue the SET statement before creating the table:
SET ANSI_NULL_DFLT_ON ON

You can also set a default for the db, which will cover you unless a setting connection overrides it:
ALTER DATABASE <db_name> SET ANSI_NULL_DEFAULT ON


Best is to explicitly specify NULL or NOT NULL for every column.  Then you don't have to mess with all the other possible settings.
0
 

Author Comment

by:dthansen
ID: 40332468
Thank you Scott. It sounds like there is no way for me to prevent the individual connection from overriding so I better explicitly set the value each time.

Thanks,
Dean
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question