Solved

Default of null vs. not null in Temp tables

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

617 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