Monday, February 20, 2012

parameterized query on sql 2005,2000 strange, simple example

Hi,
Can somebody please explain why the bellow described difference happens when
executing the same query with parameters and without, db set to
compatibility level 2000, so the same happens on sql 2000:
This query with parameters returns some result, although it should return
none:
exec sp_executesql N'select distinct top 50 name from LOCATION where (name
like @.ParamVal) and ( name < @.ParamLess ) order by 1 desc',N'@.ParamVal
nvarchar(4000),@.ParamLess
nvarchar(4000)',@.ParamVal=N'A%',@.ParamLess=N'A-100'
If I substitute parameters with values it returns proper result, i.e.
doesn't return anything as it should be:
select distinct top 50 name from LOCATION where (name like 'A%') and ( name
< 'A-100' ) order by 1 desc
Thank you
Vadim
Figured this out myself,
Thank you
"Vadim" <vadim@.dontsend.com> wrote in message
news:uJIRy1mOHHA.2232@.TK2MSFTNGP02.phx.gbl...
> Hi,
> Can somebody please explain why the bellow described difference happens
> when executing the same query with parameters and without, db set to
> compatibility level 2000, so the same happens on sql 2000:
> This query with parameters returns some result, although it should return
> none:
> exec sp_executesql N'select distinct top 50 name from LOCATION where (name
> like @.ParamVal) and ( name < @.ParamLess ) order by 1 desc',N'@.ParamVal
> nvarchar(4000),@.ParamLess
> nvarchar(4000)',@.ParamVal=N'A%',@.ParamLess=N'A-100'
> If I substitute parameters with values it returns proper result, i.e.
> doesn't return anything as it should be:
> select distinct top 50 name from LOCATION where (name like 'A%') and (
> name < 'A-100' ) order by 1 desc
> Thank you
> Vadim
>
>

No comments:

Post a Comment