3

我在自动完成下拉列表中使用存储过程来检索客户端列表。一些客户的名称中有一个“&”(和号)符号,例如“H&M”、“Marks & Spencers”,用户希望在“&”符号上进行搜索。

当我直接使用 & 符号时,它会将其用作分词器,并且不会选择具有 '&' 符号的列表。

有什么方法可以使用“&”搜索表并检索具有“&”符号的值。

希望我已经解释了我需要做什么。

感谢你的帮助!

4

2 回答 2

2

也许是这样的:

WHERE column LIKE REPLACE(@searchvalue, '&', '/&') ESCAPE '/'

另一个想法:

DECLARE @randomstring @text

SET @randomstring ='randomstringthatwillneverbeusedforsearch'

WHERE REPLACE(column, @searchvalue, @randomstring) LIKE '%'+@randomstring+'%'

虽然不知道性能问题。

于 2013-04-12T09:55:19.350 回答
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
于 2013-04-12T09:49:09.563 回答