0

我正在尝试使用以下查询检索一些数据,并且我想对其进行参数化。

select Col A,Col B,Col C,Col D
From TableA
where (COL A like '01%' or COL A like '02%')

我正在使用以下查询,但没有得到输出。

declare @p1 varchar ='01%',
        @p2 varchar ='02%'
select Col A,Col B,Col C,Col D
From TableA
where (COL A like @p1 or COL A like @p2)

请建议如何使用 sql server 2008r2 参数化上述查询的更好方法。

谢谢

4

1 回答 1

4

The problem is that you did not specify a length for your parameters, and they are being resolved as varchar(1). You could see this with debugging 101, e.g.

DECLARE @p1 VARCHAR = '01%';
SELECT p1 = @p1, lenp1 = DATALENGTH(@p1);

Result:

p1   lenp1
---- -----
0    1

Try:

DECLARE @p1 VARCHAR(32) = '01%',
        @p2 VARCHAR(32) = '02%';
...

Please be explicit instead of lazy. Also use semi-colons to terminate statements.

于 2013-03-20T14:16:25.027 回答