Nulls and SQL Server 2008
3 posters
Page 1 of 1
Nulls and SQL Server 2008
I am having trouble inserting data with nulls into a dimension table with SQl Server 2008. I've changed the default values for the columns in the dimension table to be the "dummy" values - NA, 00, Not Available, etc. But, when I insert data with nulls into the table, it doesn't replace the the null values with the default. It's inserting the null value. If I change the definition of the column to NOT NULL, then the load bombs.
I figure I can either define the the default values in the ETL software or do case statements to overcome this issue, but it makes me wonder why I bothered to change the default values of the table.
So I ask, what would Ralph do?
Is there a way to change the DDL to make it automatically insert the default value when it encounters a null during the insert? Do I have to use something other than an INSERT INTO. Do I use a combination of ISNULL and CASE STATEMENTS in the SQL? Or should I define it in the ETL software (Data Manager)?
I figure I can either define the the default values in the ETL software or do case statements to overcome this issue, but it makes me wonder why I bothered to change the default values of the table.
So I ask, what would Ralph do?
Is there a way to change the DDL to make it automatically insert the default value when it encounters a null during the insert? Do I have to use something other than an INSERT INTO. Do I use a combination of ISNULL and CASE STATEMENTS in the SQL? Or should I define it in the ETL software (Data Manager)?
Jeff Smith- Posts : 471
Join date : 2009-02-03
Re: Nulls and SQL Server 2008
The point is the declarative DEFAULT value would only apply if the column is left out in your insert list. So if you have NULL in your insert, you mean to put NULL in the column instead of default value. Simple solution, enforce the default values in your ETL by COALESCE function instead of table declaration.
hang- Posts : 528
Join date : 2010-05-07
Location : Brisbane, Australia
Re: Nulls and SQL Server 2008
You're awesome. You've added hours to my life. Thanks.
Jeff Smith- Posts : 471
Join date : 2009-02-03
Re: Nulls and SQL Server 2008
Do you use ISNULL when you can and COALESCE when isnull isn't an option? Or would you use COALESCE consistently?
I was wonder if there was a performance benefit of the ISNULL or vice versa.
If there is no difference, then I would want to use COALESCE just to keep it consistent.
I was wonder if there was a performance benefit of the ISNULL or vice versa.
If there is no difference, then I would want to use COALESCE just to keep it consistent.
Jeff Smith- Posts : 471
Join date : 2009-02-03
Re: Nulls and SQL Server 2008
Consistency within your work is one thing, but more importantly, COALESCE is an ANSI standard complied by all major DB vendors. Although less known and hard to spell, COALESCE can take more than two parameters, hence more powerful. I guess ISNULL, like the old way of joining tables, ie. FROM table1, table2 instead of JOIN ... ON, still exists purely for backward compatibility, and should be replaced by COALESCE in all new codes.
hang- Posts : 528
Join date : 2010-05-07
Location : Brisbane, Australia
Re: Nulls and SQL Server 2008
Yeah, I make it a point to tell my developers to use COALESCE over ISNULL.
Here's a post on a performance comparison of the two:
http://sqlblog.com/blogs/adam_machanic/archive/2006/07/12/performance-isnull-vs-coalesce.aspx
Make sure you read the comments at the bottom.
In summary, Adam shows that in SQL 2008, COALESCE is slightly faster (not the case in 2005).
Here's a post on a performance comparison of the two:
http://sqlblog.com/blogs/adam_machanic/archive/2006/07/12/performance-isnull-vs-coalesce.aspx
Make sure you read the comments at the bottom.
In summary, Adam shows that in SQL 2008, COALESCE is slightly faster (not the case in 2005).
Re: Nulls and SQL Server 2008
Thanks guys. I'm a convert.
It's funny how learning a new function can brighten a day.
It's funny how learning a new function can brighten a day.
Jeff Smith- Posts : 471
Join date : 2009-02-03
Similar topics
» 1 instance or 2 in SQL Server 2008
» ETL from Oracle to SQL Server 2008 Data Warehouse
» Fact Indexing -SQL Server 2008
» What are some Best Practices to ETL from Excel to SQL Server 2008?
» SK generation in SQL Server 2005/2008
» ETL from Oracle to SQL Server 2008 Data Warehouse
» Fact Indexing -SQL Server 2008
» What are some Best Practices to ETL from Excel to SQL Server 2008?
» SK generation in SQL Server 2005/2008
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum