Solved

Default of null vs. not null in Temp tables

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

Expert Comment

by:Kyle Abrahams
Comment Utility
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:
ScottPletcher earned 500 total points
Comment Utility
>> 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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now