一个不大注意的存储过程的小细节。

看下面这个存储过程
create proc pGetInforArticleDataBySearch
@autoTypeCode        as varchar(6),
@autoFittingCode     as varchar(3),
@keyword            as nvarchar(10)
as
begin
set @autoTypeCode='%'+@autoTypeCode+'%'
set @autoFittingCode='%'+@autoFittingCode+'%'
set @keyword='%'+@keyword+'%'
SELECT * FROM dbo.vinfoArticle
where autoTypeCode like @autoTypeCode and autoFittingCode like @autoFittingCode and title like @keyword
end
我们可以看到,入参重新设置为like的使用值,这个时候,如果@autoTypeCode的入参值长度为6位的时候,例如:100001,set @autoTypeCode='%'+@autoTypeCode+'%'以后@autoTypeCode就变成了%10000,自然就匹配不到了。
所以我们必须把@autoTypeCode的长度设置的长一些,不能和数据库字段的长度相同。
例如:
create proc pGetInforArticleDataBySearch
@autoTypeCode        as varchar(10),
@autoFittingCode     as varchar(10),
@keyword            as nvarchar(12)
as
begin
set @autoTypeCode='%'+@autoTypeCode+'%'
set @autoFittingCode='%'+@autoFittingCode+'%'
set @keyword='%'+@keyword+'%'
SELECT * FROM dbo.vinfoArticle
where autoTypeCode like @autoTypeCode and autoFittingCode like @autoFittingCode and title like @keyword
end
原文地址:https://www.cnblogs.com/ami/p/548753.html