tanathos
tanathos

Reputation: 5606

SQL Server 2008 subquery with error. Still works the main query

Ok, maybe I'm a noob in sql, but I can't figure out why this should work:

I've Table1 like:

IDRecord (PK), Description, IDTable2 (FK)

and a Table2 like this:

IDRecord (PK), Description

with Table1.IDTable2 as FK to Table2.IDRecord.

Then I've a very simple query:

select * from Table1
where IDTable2 not in (select IDMispelledRecord from Table2)

I've made a syntax error! There's not a column called IDMispelledRecord in Table2, and if I execute the subquery alone, it returns to me

Invalid column name 'IDMispelledRecord'.

But if I execute the WHOLE query it doesn't raise an error, simply returns 0 rows.

Can anyone tell me why?

Upvotes: 2

Views: 181

Answers (1)

Martin Smith
Martin Smith

Reputation: 453027

Though your question doesn't show this (please post your actual code in future) Table1 must have a column called IDMispelledRecord.

Your subquery is referencing that column from the outer query.

Upvotes: 2

Related Questions