我该如何转换此代码:
SELECT * FROM DB
WHERE field LIKE N'%'+RTRIM(LTRIM('98'))+N'%'
为:
SET @stringWhere= ' SELECT * FROM DB AND field LIKE '+N'%' +RTRIM(LTRIM(@field))+ N'%';
答案 0 :(得分:1)
有点猜测。请下面的代码。并确保查看代码中的注释,因为有几个方面可以使用改进。
declare @field nvarchar(10) = N'98'
select * --don't use *, select ONLY the columns you need
from DB
where Name like '%' + @field+ '%' --be careful here, this is a nonSARGable predicate with the leading wildcard. As such all indexes will be rendered useless.
答案 1 :(得分:1)
我找到了回答我的问题,如果我想在SQL中使用字符串格式设置SELECT命令,我们可以使用:
SET @stringWhere= 'SELECT * FROM DB AND field LIKE '+'N''%'+ RTRIM(LTRIM(@field))+'%'''+' ' ;