473,545 Members | 2,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inconsistent SQL results

Hi

I have an oddity. If I run a piece of SQL:

SELECT EmployeeNo, MailTo
FROM ST_PPS.dbo.Empl oyee
where AddedOn BETWEEN '01-jan-2006' and '01-feb-2006'
AND MailTo NOT IN ( '3', 'x')
order by MailTo

I get the results

EmployeeNo MailTo
----------- ------
608384 1
606135 1
608689 1
609095 1
607163 1
606165 1
606472 1
608758 1
.....
for 2594 rows

If I create a stored procedure with the same SQL:-

CREATE PROCEDURE dbo.PPS_test
AS
SELECT EmployeeNo, MailTo
FROM ST_PPS.dbo.Empl oyee
where AddedOn BETWEEN '01-jan-2006' and '01-feb-2006'
AND MailTo NOT IN ( '3', 'x')
order by MailTo
GO

and run it:-

EXEC PPS_test

I get three extra rows

EmployeeNo MailTo
----------- ------
607922 NULL
606481 NULL
605599 NULL
606316 1
608871 1
607427 1
608795 1
.....
for 2597

Does anyone know what is happening here? It appears that the clause:-

MailTo NOT IN ( '3', 'x')

excludes NULL in raw SQL, but includes NULL (correctly I think) in a
stored procedure.

Chloe Crowder
The British Library

Apr 11 '06 #1
5 3013
verify your ansi_nulls settings:

create table #t(i int)
insert into #t
select null
union all
select 1
union all
select 2
union all
select 3
go
select i from #t where i not in(1)

i
-----------
2
3

(2 row(s) affected)
go
set ansi_nulls off
go
select i from #t where i not in(1)

i
-----------
NULL
2
3

(3 row(s) affected)

go
drop table #t

Apr 11 '06 #2
ch***********@b l.uk wrote:
Hi

I have an oddity.


Well, I'm not able to duplicate your problem with the sql below. Can
you possibly provide create table and insert statements that show the
strange behavior?

create table Employee (
EmployeeNo int,
MailTo char(1) NULL
)
insert Employee values (608384,'1')
insert Employee values (606135,'1')
insert Employee values (608689,'x')
insert Employee values (609095,'3')
insert Employee values (607922,'2')
insert Employee (EmployeeNo) values (606481)
insert Employee (EmployeeNo) values (605599)

select EmployeeNo, MailTo
from Employee
where MailTo NOT IN ( '3', 'x')
order by MailTo
go

create procedure PPS_test as
select EmployeeNo, MailTo
from Employee
where MailTo NOT IN ( '3', 'x')
order by MailTo
go

exec PPS_test

drop table Employee
drop procedure PPS_test

Apr 11 '06 #3
(ch***********@ bl.uk) writes:
If I create a stored procedure with the same SQL:-

CREATE PROCEDURE dbo.PPS_test
AS
SELECT EmployeeNo, MailTo
FROM ST_PPS.dbo.Empl oyee
where AddedOn BETWEEN '01-jan-2006' and '01-feb-2006'
AND MailTo NOT IN ( '3', 'x')
order by MailTo
GO

and run it:-

EXEC PPS_test

I get three extra rows

EmployeeNo MailTo
----------- ------
607922 NULL
606481 NULL
605599 NULL


Let me guess: you are creating your stored procedures in Enterprise
Manager, aren't you? That's a crappy tool to edit stored procedures
in. You are better off using Query Analyzer.

One reason it's crappy is because, it defaults the settings
ANSI_NULLS and QUOTED_IDENTIFI ER to be OFF. These settings are
saved with the procedure, so when you run the procedure ANSI_NULLS
is off, and you get three extra rows. Normally, when ANSI_NULLS is
ON (which is the default in most environments), NULL is never
equal to anything, and never is it unequal to anything. But when the
setting is OFF NULLs are equal to other NULLS and unequal to other
values. This is a legacy setting that should be avoided.

There are also features in SQL Server that requires ANSI_NULLS to
be ON, so there is all reason to run with ANSI_NULLS on.

In Query Analyzer, ANSI_NULLS is ON by default.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Apr 11 '06 #4
Erland

Ah, that would certainly explain it. Don't you just love Microsoft at
times!

Does the 'feature' carry forward into SQL 2005?

Thanks to Eric and Alexander for their time

Chloe

Apr 12 '06 #5
(ch***********@ bl.uk) writes:
Ah, that would certainly explain it. Don't you just love Microsoft at
times!

Does the 'feature' carry forward into SQL 2005?


The legacy settings SET ANSI_NULLS OFF and SET QUOTED_IDENTIFI ER OFF
remains. But in the new Mgmt Studio it is not equally easy to run with
these settings off. (There are alas some cases where they still turn them
off, despite that I have hammered them with bug reports about this.)
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Apr 12 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1561
by: AbeR | last post by:
I have written a fairly simple reporting app that has been working for a few years without any issues until this past week. The application has a login page with a DSN lookup for login/password validation. A confirm login asp page does a redirect back to the login page if failed or to a menu page if passed where I pull information from the...
1
1920
by: Ellen Manning | last post by:
I've got an A2K continuous form based on a query of 2 tables. The query returns results if the field "Outstanding" = Yes. The form's recordset type is set to Inconsistent Updates. If the user changes "Outstanding" to No, then the form automatically refreshes itself, displaying only the records with Outstanding set to Yes. How do I stop...
0
1182
by: bjbounce2002 | last post by:
Hello, I have a field containing OLE Objects in Access 97. The OLE Objects are links to Microsoft Word templates. I have a parameter query which uses criteria Like "*" & & "*" to bring up word files which contain a certain text. This does not bring up all results for some reason as there are template letters (.dot) which I know...
0
1612
by: Itai | last post by:
Background: I have four Web Form pages with respective C# code behind files, all in the same project: localhost/vpath1 Page1.aspx Page2.aspx
2
3338
by: HCF_15 | last post by:
Hi all, I have a small piece of code to use StackFrame to GetMethod, I found it is inconsistent in Debug version and Release version, is there anything I am doing wrong? Here is code, build in Debug and Release version, you will get different results. My env. is VS 2005 on XP with SP2. Thanks in advanced.
2
2585
by: robert maas, see http://tinyurl.com/uh3t | last post by:
(second attempt) I'm just a beginner at perl. Currently I'm comparing how integers are supported in several different programming languages: <http://www.rawbw.com/~rem/HelloPlus/CookBook/CookTop.html#int> I wrote a test program in perl, but it gives inconsistent results. Here's the program source: #!/usr/bin/perl $a = 'Hello'; $b =...
20
2584
by: Francine.Neary | last post by:
I am learning C, having fun with strings & pointers at the moment! The following program is my solution to an exercise to take an input, strip the first word, and output the rest. It works fine when you give it 2 or more words, but when there's only 1 word the results vary depending on whether it's on Windows or Linux: under MSVC it displays...
1
4265
by: catudalg | last post by:
Configuration: Windows 2000 sp3 MS Access 2000 (9.0.4402 SR-1) Visual Foxpro 9.0 detached tables MS VFP Driver 6.01.6830.01 06/19/2003 For example, a simple query like: select * from ddwg1 union all select * from ddwg2
3
6175
by: Rahul Babbar | last post by:
Hi All, When could be the possible reasons that could make a database inconsistent? I was told by somebody that it could become inconsistent if you " force all the applications to a close on that database and transactions are not committed or rollbacked" I infact, don't believe it, because i suppose when we do a "force
1
3275
by: rnhuch | last post by:
My platform is SQL Server 2005. One of the tables of my db has 28589928 rows and one of the fields is real. When I backup and restore this database to another server (using the SQL Server internal tool) and do a SELECT SUM(<<field name>>) from <<table name>>, the results from these two servers are slightly different. I don't understand the...
0
7432
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7943
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7456
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7786
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6022
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5359
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3490
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1919
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.