Solved

Default of null vs. not null in Temp tables

Posted on 2014-09-18
3
414 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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 retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

734 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