FIX: SQL Server Might Return Incorrect Results When You Use an Outer Join and a View (321541)



The information in this article applies to:

  • Microsoft SQL Server 2000 (all editions)

This article was previously published under Q321541
BUG #: 357109 (SHILOH_BUGS)

SYMPTOMS

SQL Server returns incorrect results in a particular scenario that involves a query with an outer join and a view.

You can reproduce the behavior when all the following conditions exist:
  • You use a query that contains an outer join, and you use a view (directly or indirectly) on the inner side of the outer join.
  • The view contains an expression in its SELECT list that references one or more base table columns. Also, if all base table column values are NULL, the expression returns non-NULL results.
  • The view is a simple view that does not contain a DISTINCT, TOP, or GROUP BY aggregate.

CAUSE

An incorrect view resolution is causing the problem.

When the condition in the first bullet item of the "Symptoms" section is true, the inlining process will involve pulling all result expressions of the view from under the outer join to be computed above the outer join.

When the condition in the second bullet item of the "Symptoms" section is true, this pulling is semantically incorrect and will lead to incorrect results.

When the condition in the third bullet item of the "Symptoms" section is true, the view resolution code will inline the view tree in the query.

RESOLUTION

To resolve this problem, obtain the latest service pack for Microsoft SQL Server 2000. For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

290211 INF: How To Obtain the Latest SQL Server 2000 Service Pack

NOTE: The following hotfix was created before the release of Microsoft SQL Server 2000 Service Pack 3.

A supported fix is now available from Microsoft, but it is only intended to correct the problem that is described in this article. Apply it only to computers that are experiencing this specific problem. This fix may receive additional testing. Therefore, if you are not severely affected by this problem, Microsoft recommends that you wait for the next Microsoft SQL Server 2000 service pack that contains this hotfix.

To resolve this problem immediately, contact Microsoft Product Support Services to obtain the fix. For a complete list of Microsoft Product Support Services phone numbers and information about support costs, visit the following Microsoft Web site:NOTE: In special cases, charges that are ordinarily incurred for support calls may be canceled if a Microsoft Support Professional determines that a specific update will resolve your problem. The typical support costs will apply to additional support questions and issues that do not qualify for the specific update in question.

The English version of this fix should have the following file attributes or later:
   Date         Version    Size       File name
   ------------------------------------------------

   04/20/2002   8.00.613   7,281 KB   Sqlservr.exe 
				
NOTE: Because of file dependencies, the most recent hotfix or feature that contains the files may also contain additional files.


STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.

This problem was first corrected in Microsoft SQL Server 2000 Service Pack 3.

MORE INFORMATION

To reproduce the problem, use the following script:

create table t1 (pk1 int not null,primary key (pk1))
create table t2 (pk1 int not null,label1 varchar(10) not null,primary key (pk1))
go
insert into t1 values (1)
insert into t2 values (2, 'XXXXX')
go
create view V as
select pk1,
       case  when label1 is null then  'b2 mapped 1' else label1 end as label1 from t2
go
select A.pk1 as A_pk1, B.pk1 as B_pk1, B.label1 as B_label1
from t1 as A left outer join V as B on A.pk1 = B.pk1
go
				

The script returns this result:

A_pk1       B_pk1       B_label1    
----------- ----------- -----------
1           NULL        b2 mapped 1
				


This is the result the SELECT statement is supposed to return:

A_pk1       B_pk1       B_label1    
----------- ----------- -----------
1           NULL        NULL
				



Modification Type:MinorLast Reviewed:10/11/2005
Keywords:kbHotfixServer kbQFE kbbug kbfix KB321541 kbAudDeveloper