How hard is it to incorporate full text search with SQL Server?

前端 未结 5 922
被撕碎了的回忆
被撕碎了的回忆 2020-12-29 09:19

I am building a C#/ASP.NET app with an SQL backend. I am on deadline and finishing up my pages, out of left field one of my designers incorporated a full text search on one

5条回答
  •  长情又很酷
    2020-12-29 10:04

    I have used dtSearch before for adding full text searching to files and databases, and their stuff is pretty cheap and easy to use.

    Short of adding all that and configuring SQL, This script will search through all columns in a database and tell you what columns contain the values you are looking for. I know its not the "proper" solution, but may buy you some time.

    /*This script will find any text value in the database*/
    /*Output will be directed to the Messages window. Don't forget to look there!!!*/
    
    SET NOCOUNT ON
    DECLARE @valuetosearchfor varchar(128), @objectOwner varchar(64)
    SET @valuetosearchfor = '%staff%' --should be formatted as a like search 
    SET @objectOwner = 'dbo'
    
    DECLARE @potentialcolumns TABLE (id int IDENTITY, sql varchar(4000))
    
    INSERT INTO @potentialcolumns (sql)
    SELECT 
        ('if exists (select 1 from [' +
        [tabs].[table_schema] + '].[' +
        [tabs].[table_name] + 
        '] (NOLOCK) where [' + 
        [cols].[column_name] + 
        '] like ''' + @valuetosearchfor + ''' ) print ''SELECT * FROM [' +
        [tabs].[table_schema] + '].[' +
        [tabs].[table_name] + 
        '] (NOLOCK) WHERE [' + 
        [cols].[column_name] + 
        '] LIKE ''''' + @valuetosearchfor + '''''' +
        '''') as 'sql'
    FROM information_schema.columns cols
        INNER JOIN information_schema.tables tabs
            ON cols.TABLE_CATALOG = tabs.TABLE_CATALOG
                AND cols.TABLE_SCHEMA = tabs.TABLE_SCHEMA
                AND cols.TABLE_NAME = tabs.TABLE_NAME
    WHERE cols.data_type IN ('char', 'varchar', 'nvchar', 'nvarchar','text','ntext')
        AND tabs.table_schema = @objectOwner
        AND tabs.TABLE_TYPE = 'BASE TABLE'
    ORDER BY tabs.table_catalog, tabs.table_name, cols.ordinal_position
    
    DECLARE @count int
    SET @count = (SELECT MAX(id) FROM @potentialcolumns)
    PRINT 'Found ' + CAST(@count as varchar) + ' potential columns.'
    PRINT 'Beginning scan...'
    PRINT ''
    PRINT 'These columns contain the values being searched for...'
    PRINT ''
    DECLARE @iterator int, @sql varchar(4000)
    SET @iterator = 1
    WHILE @iterator <= (SELECT Max(id) FROM @potentialcolumns)
    BEGIN
        SET @sql = (SELECT [sql] FROM @potentialcolumns where [id] = @iterator)
        IF (@sql IS NOT NULL) and (RTRIM(LTRIM(@sql)) <> '')
        BEGIN
            --SELECT @sql --use when checking sql output
            EXEC (@sql)
        END
        SET @iterator = @iterator + 1
    END
    
    PRINT ''
    PRINT 'Scan completed'
    

提交回复
热议问题