Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««12345»»

Nulls Expand / Collapse
Author
Message
Posted Thursday, December 9, 2010 1:29 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 31, 2013 8:01 AM
Points: 1,232, Visits: 1,046
Artur Komkov (12/9/2010)
What's why I always try to avoid nullable columns or make something like that:

select * from dbo.Employees where EmployeeID
not in (SELECT isnull(NullableColumn,0) FROM NullOperation)


Artur should get a gold star and extra points for using the function that was created to keep the ANSI NULL issue the question points out from occuring. I work on a database application that has 3000+ stored procedures. Almost all of them use the isnull function.
Post #1032706
Posted Thursday, December 9, 2010 5:22 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:23 AM
Points: 5,925, Visits: 8,173
Artur Komkov (12/9/2010)
What's why I always try to avoid nullable columns or make something like that:

select * from dbo.Employees where EmployeeID
not in (SELECT isnull(NullableColumn,0) FROM NullOperation)

While that would indeed avoid this issue, it also makes it a lot harder for the optimizer to use an index (if any) on the NullableColumn. Here is an alternative that will still use indexes:
SELECT   *   -- Use column list instead!
FROM dbo.Employees AS e
WHERE NOT EXISTS
(SELECT *
FROM NullOperation AS n
WHERE n.NullableColumn = e.EmployeeID);




Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #1032796
Posted Friday, December 10, 2010 2:40 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, December 5, 2013 7:19 AM
Points: 69, Visits: 65
The code returns an error:

[color=#FF0000]Msg 3701, Level 11, State 5, Line 1
Cannot drop the table 'dbo.NullOperation', because it does not exist or you do not have permission.[/color]

The table is created using default schema which the contributor has assumed to be dbo (evident in the 'drop table dbo.NullOperation' statement). My default schema is not dbo!

Having said that, I still like the question because it has proven its point - the error occurred AFTER the empty recordset was returned by the select statement.
Post #1032892
Posted Friday, December 10, 2010 6:16 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, February 20, 2014 4:13 PM
Points: 371, Visits: 250
I have tried to avoid NOT IN clauses, I couldn't remember why but here it is.
I usually use outer joins eg

select * from #Employees e
left outer join #NullOperation n
on e.EmployeeID = n.NullableColumn
where n.NullableColumn IS NULL

so that I get what I expect, which is what we all want!
Post #1032964
Posted Friday, December 10, 2010 7:31 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 31, 2013 8:01 AM
Points: 1,232, Visits: 1,046
phil.wood 94423 (12/10/2010)

My default schema is not dbo!


Most QOTD readers that actually run or test the QOTD scripts do so in a default NON Production installation of SQL server that is easily rebuilt. Think Virtual PC with a Snapshot, or a server with a good Ghost backup image.

Also a database where you have DBO schema access is prefered and taken for granted, unless otherwise stated in the script or example code.

Good thing the QOTD was not about a script to clear all event and server logs and clean the DMV's of historical data...
Post #1033009
Posted Sunday, December 12, 2010 6:29 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 4:45 AM
Points: 8,559, Visits: 9,054
Good question with clear answer options, but as several have pointed out the explanation is not a real explanation.

Tom
Post #1033466
Posted Monday, December 13, 2010 2:15 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, December 5, 2013 7:19 AM
Points: 69, Visits: 65
SanDroid (12/10/2010)
phil.wood 94423 (12/10/2010)

My default schema is not dbo!


Most QOTD readers that actually run or test the QOTD scripts do so in a default NON Production installation of SQL server that is easily rebuilt. Think Virtual PC with a Snapshot, or a server with a good Ghost backup image.

Also a database where you have DBO schema access is prefered and taken for granted, unless otherwise stated in the script or example code.

Good thing the QOTD was not about a script to clear all event and server logs and clean the DMV's of historical data...


Isn't it normal to have development environments, virtual or otherwise, set up as similarly as possible to production? It should be.
Post #1033647
Posted Tuesday, December 14, 2010 10:31 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 10:18 AM
Points: 21,221, Visits: 14,922
Thanks for the question.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #1034576
Posted Tuesday, December 14, 2010 11:10 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 31, 2013 8:01 AM
Points: 1,232, Visits: 1,046
phil.wood 94423 (12/13/2010)
SanDroid (12/10/2010)
phil.wood 94423 (12/10/2010)

My default schema is not dbo!


Most QOTD readers that actually run or test the QOTD scripts do so in a default NON Production installation of SQL server that is easily rebuilt. Think Virtual PC with a Snapshot, or a server with a good Ghost backup image.

Also a database where you have DBO schema access is prefered and taken for granted, unless otherwise stated in the script or example code.

Good thing the QOTD was not about a script to clear all event and server logs and clean the DMV's of historical data...


Isn't it normal to have development environments, virtual or otherwise, set up as similarly as possible to production? It should be.


My point is don't blame the author just becuase you do not understand how his script will run in your custom environment.

Of course your Dev environment should match your prod.
But I would not run any QOTD code, or any code from the internet, in a database supporting Development of a specific application either.
Post #1034601
Posted Tuesday, December 14, 2010 12:23 PM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Monday, July 14, 2014 10:08 AM
Points: 754, Visits: 3,816
Good question - thanks! NULLs can be hazardous, they should come with a warning.

______________________________________________________________________
The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge. - Stephen Hawking
Post #1034645
« Prev Topic | Next Topic »

Add to briefcase «««12345»»

Permissions Expand / Collapse