FIX: SELECT DISTINCT from Table with LEFT JOIN of View Causes Error Messages or Client Application May Stop Responding (308547)



The information in this article applies to:

  • Microsoft SQL Server 2000 (all editions)

This article was previously published under Q308547
BUG #: 355640 (SHILOH_BUGS)

SYMPTOMS

A SELECT DISTINCT query that joins a table to a view through an outer join may:
  • Cause error messages or exceptions at the client.
  • Produce the appearance of incorrect results.
  • Cause the client application to stop responding (hang).
The error messages that may occur include:
Bad token from SQL Server: Datastream processing out of sync.

-or-

Unknown token received from SQL Server

-or-

Protocol error in TDS stream

-or-

Invalid cursor state

-or-

Function sequence error

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

Hotfix

NOTE: The following hotfix was created prior to Microsoft SQL Server 2000 Service Pack 2.

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

   Sqlservr.exe   8.00.452   i386
				
NOTE: Due to file dependencies, the most recent hotfix or feature that contains the preceding files may also contain additional files.


WORKAROUND

To work around this problem either:
  • Remove the DISTINCT keyword from the query, or select from the base table instead of from a view that references the table.

    -or-

  • Change the nullability of the columns in the base table that are selected by the query (the problem arises when the nullability of column data differs from the metadata reported to the client).

STATUS

Microsoft has confirmed that this is a problem in Microsoft SQL Server 2000. This problem was first corrected in Microsoft SQL Server 2000 Service Pack 2.

MORE INFORMATION

The following script demonstrates the problem:
use master
go
drop database dbtest
create database dbtest 
use dbtest
go
create table dbtest (
  c1 smallint not null primary key, 
  c2 char(30) null) 
create table dbtest2 (
  c1 smallint not null,
  c2 smallint not null,
  c3 char(30) null)
insert into dbtest(c1,c2) values (1,'abcd')
insert into dbtest(c1,c2) values (2,'abcdef')
insert into dbtest(c1,c2) values (3,'acdef')
insert into dbtest2(c1,c2,c3) values (1,1,'abcd')
go
create view dbview as select * from dbtest.dbo.dbtest
go
select distinct dbview.c1,dbview.c2 from dbview 
left join dbtest2 on dbtest2.c1 = dbview.c1
go
				

Modification Type:MinorLast Reviewed:9/26/2005
Keywords:kbHotfixServer kbQFE kbbug kbfix kbSQLServ2000preSP2Fix KB308547