tags:

views:

72

answers:

2

I have 2 similar queries

select * 
from openquery(powerschool, 
                'select * 
                 from TEACHERS 
                 where teachernumber is not null 
                   and schoolid=''1050'' 
                   and teacherloginid is not null  
                 order by teachernumber')

and

SELECT * 
from openquery(powerschool, 
              'SELECT NVL(teachernumber,'''') 
               from TEACHERS 
               where teachernumber is not null 
                 and schoolid=''1050'' 
                 and teacherloginid is not null 
               order by teachernumber')

The first one is giving me 182 rows while the second one gives me 83. What's wrong with the queries?

A: 

Second query never would return a null for the teachers table because of the NVL() so it could return more records depending on the data.

basically the " and teacherloginid is not null " never gets hit because you replace the nulls with ""

Jay
Is the WHERE clause not evaluated before the SELECT clause... aka NVL is evalauted after "teachernumber is not null"
gbn
I think someone edited those queries since I commented. Wasn't the NVL in the where clause originally?
Jay
Even if I remove the NVL and keep the "TeacherNumber is not null" in the where clause the second query gives me 83 rows while the first one returns 182 rows.
Mithil Deshmukh
@Jay: not according to revision history
gbn
@gbn How do I see the history? I can see it was edited, where's the history?
Jay
@Jay: Perhaps it's a reputation thing: "edited yesterday", to the left of "asked yesterday"
gbn
A: 

Just thoughts...

  • Same server? That is, linked server is different in target or credentialss o you are reading a different "TEACHERS" table

  • What does running both linked SQL statement actually on the linked server (not locally) give you?

gbn