RE: Different Row Counts From Two Virtually Identical Queries

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Xavier Lazard (anonymous_at_discussions.microsoft.com)
Date: 03/19/04


Date: Fri, 19 Mar 2004 08:56:12 -0800

The two queries are not comparing the same thing due to the extra quotation marks:
see below

create table ##junk ( LastName nvarchar(100) )
go
insert into ##junk values ('smith')
go

declare @s varchar(4000)
declare @sLastName nvarchar(100)

select @sLastName = 'smith'

select count1 = count(*) FROM ##junk
WHERE (LastName = @sLastName )

select @sLastName = '''smith'''

select @s =
'select count2 = count(*) FROM ##junk ' +
'WHERE (LastName = ' + @sLastName + ') '
exec (@s)

drop table ##junk
go



Relevant Pages