Solved

Default of null vs. not null in Temp tables

Posted on 2014-09-18
3
402 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Replication question 9 42
migrate a SQL 2008 to 2016, 2 28
Help Required 3 96
Connecting to multiple databases to create a Dashboard 5 25
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

770 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