我在自动完成下拉列表中使用存储过程来检索客户端列表。一些客户有'&' (&符号)登录名称,例如'H& M','Marks&斯宾塞和用户想要搜索'&'登录。
当我直接使用&签名它使用它作为一个断字符,并没有选择具有'&'的列表登录。
有什么方法可以使用'&'搜索表格以及具有'&'的后悔价值登录。
希望我能够解释我需要做什么。
感谢您的帮助!
答案 0 :(得分:2)
也许是这样的:
WHERE column LIKE REPLACE(@searchvalue, '&', '/&') ESCAPE '/'
另一个想法:
DECLARE @randomstring @text
SET @randomstring ='randomstringthatwillneverbeusedforsearch'
WHERE REPLACE(column, @searchvalue, @randomstring) LIKE '%'+@randomstring+'%'
不知道性能问题。
答案 1 :(得分:0)
试试这个 -
DECLARE @char CHAR(1)
SELECT @char = '&'
DECLARE @sql NVARCHAR(MAX)
SELECT @sql = (
SELECT CHAR(13) +
'IF EXISTS(SELECT 1 FROM ' +
QUOTENAME(s.name) + '.' + QUOTENAME(o.name) +
' WHERE' + b.cols + ')
SELECT table_name = ''[' + s.name + '].[' + o.name + ']'' ' + c.cols +' FROM ' +
QUOTENAME(s.name) + '.' + QUOTENAME(o.name) +
' WHERE' + b.cols + ';'
FROM sys.objects o
JOIN sys.schemas s ON o.[schema_id] = s.[schema_id]
JOIN (
SELECT DISTINCT p.[object_id]
FROM sys.partitions p
WHERE p.[rows] > 0
) p ON p.[object_id] = o.[object_id]
OUTER APPLY (
SELECT cols = STUFF((
SELECT 'OR CHARINDEX(''' + @char + ''', ' + QUOTENAME(c.name) + ') > 0 '
FROM sys.columns c
JOIN sys.types t ON c.user_type_id = t.user_type_id
WHERE t.name IN ('char', 'nchar', 'ntext', 'nvarchar', 'text', 'varchar')
AND c.[object_id] = o.[object_id]
ORDER BY c.column_id
FOR XML PATH(''), TYPE, ROOT).value('root[1]', 'NVARCHAR(MAX)'), 1, 2, '')
) b
OUTER APPLY (
SELECT cols = (
SELECT ', ' + QUOTENAME(c.name) + ' '
FROM sys.columns c
JOIN sys.types t ON c.user_type_id = t.user_type_id
WHERE t.name IN ('char', 'nchar', 'ntext', 'nvarchar', 'text', 'varchar')
AND c.[object_id] = o.[object_id]
ORDER BY c.column_id
FOR XML PATH(''), TYPE, ROOT).value('root[1]', 'NVARCHAR(MAX)')
) c
WHERE o.[type] = 'U'
AND b.cols IS NOT NULL
FOR XML PATH(''), TYPE, ROOT).value('root[1]', 'NVARCHAR(MAX)')
EXEC sys.sp_executesql @sql
此查询在所有包含带有文本字段的表的数据库中搜索子字符串 -
DECLARE @phrase NVARCHAR(100)
SELECT @phrase = '&'
DECLARE
@db_name SYSNAME
, @output NVARCHAR(200)
DECLARE db CURSOR READ_ONLY FAST_FORWARD LOCAL FOR
SELECT d.name
FROM sys.databases d
WHERE d.state_desc = 'ONLINE'
AND d.name NOT IN ('tempdb', 'model', 'msdb', 'master')
--AND d.name = 'your db name'
ORDER BY d.name
OPEN db
FETCH NEXT FROM db INTO @db_name
WHILE @@FETCH_STATUS = 0 BEGIN
SELECT @output = CONVERT(NVARCHAR(15), GETDATE(), 114) + ': Find in ' + QUOTENAME(@db_name) + ':'
RAISERROR(@output, 0, 1) WITH NOWAIT
DECLARE @tsql NVARCHAR(MAX) = '
USE [' + @db_name + '];
DECLARE @sql NVARCHAR(MAX)
SELECT @sql = ''USE [' + @db_name + '];'' + (
SELECT
CHAR(13) +
''IF EXISTS(SELECT 1 FROM '' +
QUOTENAME(s.name) + ''.'' + QUOTENAME(o.name) +
'' WHERE'' + b.cols + '') PRINT ''''['' +
s.name + ''].['' + o.name + '']'''';''
FROM sys.objects o
JOIN sys.schemas s ON o.[schema_id] = s.[schema_id]
JOIN (
SELECT DISTINCT p.[object_id]
FROM sys.partitions p
WHERE p.[rows] > 0
) p ON p.[object_id] = o.[object_id]
OUTER APPLY (
SELECT cols = STUFF((
SELECT ''OR CHARINDEX(''''' + @phrase + ''''', '' + QUOTENAME(c.name) + '') > 0 ''
FROM sys.columns c
JOIN sys.types t ON c.user_type_id = t.user_type_id
WHERE t.name IN (''char'', ''nchar'', ''ntext'', ''nvarchar'', ''text'', ''varchar'')
AND c.[object_id] = o.[object_id]
ORDER BY c.column_id
FOR XML PATH(''''), TYPE, ROOT).value(''root[1]'', ''NVARCHAR(MAX)''), 1, 2, '''')
) b
WHERE o.[type] = ''U''
AND b.cols IS NOT NULL
FOR XML PATH(''''), TYPE, ROOT).value(''root[1]'', ''NVARCHAR(MAX)'')
EXEC sys.sp_executesql @sql
'
EXEC sys.sp_executesql @tsql
PRINT REPLICATE('-', 100) + CHAR(13)
FETCH NEXT FROM db INTO @db_name
END
CLOSE db
DEALLOCATE db