我有一个参数传入用户输入,然后该参数进入查询以进行通配符搜索。
如同:
DECLARE @myVariableContainingUserInput VARCHAR(50) = 'part of string';
DECLARE @myTable TABLE (column1 varchar(200));
INSERT INTO @myTable(column1)
VALUES ('This is the whole string that contains part of string the user is looking for')
SELECT column1
FROM @myTable
WHERE column1 LIKE '%' + @myVariableContainingUserInput + '%'
Run Code Online (Sandbox Code Playgroud)
在这种情况下,他们得到一排。
然而有时数据看起来像这样:
INSERT INTO @myTable(column1)
VALUES ('This is the whole string that contains [part] of string the user is looking for')
Run Code Online (Sandbox Code Playgroud)
用户有时可能会搜索
SET @myVariableContainingUserInput = '[part';
Run Code Online (Sandbox Code Playgroud)
他们希望找到一行包含“这是包含用户正在查找的字符串的[部分]的整个字符串”。并不是因为 [ 是一个特殊字符。
我知道我可以通过使用[[]或例如添加 a\并使用 …