如何为SQL LIKE在用户输入中转义特殊字符?
|
以下面的示例为例(SQL Server 2008-可以使用更多示例)。您将不得不想象
@query
是某些参数,其来源是用户输入:
DECLARE @query varchar(100)
SET @query = \'less than 1% fat\'
CREATE TABLE X ([A] VARCHAR(100))
INSERT X VALUES (\'less than 1% fat\')
INSERT X VALUES (\'less than 1% of doctors recommend this - it\'\'s full of fat!\')
SELECT * FROM X WHERE A LIKE \'%\' + @query + \'%\'
DROP TABLE X
查询状态为\'less than 1% fat\'
,但实际上得到的比我们想要的要多:
less than 1% fat
less than 1% of doctors recommend this - it\'s full of fat!
为了获得所需的行为,我将@query
更改为\'less than 1[%] fat\'
-然后仅返回第一个结果。
是否有一种标准方法为使用ѭ6的子句准备字符串,还是我必须自己滚动?
没有找到相关结果
已邀请:
2 个回复
郡晒景沧
渴翅吮斡撤