SQL - 如何在Table的所有列中搜索搜索字符串,然后显示列

时间:2014-12-03 11:11:54

标签: sql-server search

我正在尝试使用搜索字符串搜索整个数据库。为此,我需要检查表,视图,存储过程+所有表中的所有数据。

我能够搜索所有数据库对象,但是当我需要搜索表时有一点问题,我需要搜索单元格,如果搜索字符串存在于该特定单元格中,我需要显示表名,找到搜索字符串的单元格的特定行和列名称上的所有列。

我正在使用Sorna Kumar Maduraj 的代码来获取整行,我被卡住了以显示搜索字符串所在位置的列

例如

名为Searchtable的表在i中搜索的搜索字符串(假设它是AdminCd)位于2个不同列的特定行中(假设列名为ColumnA,ColumnB)

我的输出是表名 - 行数据 - 列名(ColumnA,ColumnB)

我唯一的问题是假设在同一个表中,一行包含2列中的字符串,而不同的行包含4列中的搜索字符串,我需要以不同的方式显示列作为输出

我使用的代码,生病了下面

DECLARE  @Tablenames VARCHAR(500)
DECLARE @SearchStr NVARCHAR(60)
DECLARE @GenerateSQLOnly Bit = 0
SET @Tablenames = 'TableName'
SET @SearchStr = '%AdminCd%'
    SET NOCOUNT ON
    DECLARE @MatchFound BIT -- To check wether any match is found or not 
   SELECT @MatchFound = 0
    DECLARE @CheckTableNames Table -- To check the tables that need to be searched 
(
Tablename sysname
)
DECLARE @SQLTbl TABLE -- For Table Name
(
 Tablename      SYSNAME
,WHEREClause    VARCHAR(MAX)
,SQLStatement   VARCHAR(MAX)
,Execstatus     BIT 
)
DECLARE @SQL VARCHAR(MAX)
DECLARE @tmpTblname sysname
DECLARE @ErrMsg VARCHAR(100)
IF LTRIM(RTRIM(@Tablenames)) IN ('' ,'%')
BEGIN
    INSERT INTO @CheckTableNames
    SELECT Name
      FROM sys.tables
END
ELSE
BEGIN
    SELECT @SQL = 'SELECT ''' + REPLACE(@Tablenames,',',''' UNION SELECT ''') + ''''
    INSERT INTO @CheckTableNames
    EXEC(@SQL)
END
    IF NOT EXISTS(SELECT 1 FROM @CheckTableNames)
BEGIN
    SELECT @ErrMsg = 'No tables are found in this database ' + DB_NAME() + ' for the specified filter'
    PRINT @ErrMsg
    RETURN
END
INSERT INTO @SQLTbl
( Tablename,WHEREClause)
SELECT QUOTENAME(SCh.name) + '.' + QUOTENAME(ST.NAME),
        (
            SELECT '[' + SC.Name + ']' + ' LIKE ''' + @SearchStr + ''' OR ' + CHAR(10)
              FROM SYS.columns SC
              JOIN SYS.types STy
                ON STy.system_type_id = SC.system_type_id
               AND STy.user_type_id =SC.user_type_id
             WHERE STY.name in ('varchar','char','nvarchar','nchar','text')
               AND SC.object_id = ST.object_id
             ORDER BY SC.name
            FOR XML PATH('')
        )
  FROM  SYS.tables ST
  JOIN @CheckTableNames chktbls
            ON chktbls.Tablename = ST.name 
  JOIN SYS.schemas SCh
    ON ST.schema_id = SCh.schema_id
 WHERE ST.name <> 'SearchTMP'
  GROUP BY ST.object_id, QUOTENAME(SCh.name) + '.' +  QUOTENAME(ST.NAME) ;
  UPDATE @SQLTbl
     SET SQLStatement = 'SELECT * INTO SearchTMP FROM ' + Tablename + ' WHERE ' + substring(WHEREClause,1,len(WHEREClause)-5)
  DELETE FROM @SQLTbl
   WHERE WHEREClause IS NULL
WHILE EXISTS (SELECT 1 FROM @SQLTbl WHERE ISNULL(Execstatus ,0) = 0)
BEGIN
    SELECT TOP 1 @tmpTblname = Tablename , @SQL = SQLStatement
      FROM @SQLTbl 
     WHERE ISNULL(Execstatus ,0) = 0
     IF @GenerateSQLOnly = 0
     BEGIN
        IF OBJECT_ID('SearchTMP','U') IS NOT NULL
            DROP TABLE SearchTMP
        EXEC (@SQL)
        IF EXISTS(SELECT 1 FROM SearchTMP)
        BEGIN
            SELECT Tablename=@tmpTblname,* FROM SearchTMP
            SELECT @MatchFound = 1
        END
     END
     ELSE
     BEGIN
         PRINT REPLICATE('-',100)
         PRINT @tmpTblname
         PRINT REPLICATE('-',100)
         PRINT replace(@SQL,'INTO SearchTMP','')
     END
     UPDATE @SQLTbl
        SET Execstatus = 1
      WHERE Tablename = @tmpTblname
END
IF @MatchFound = 0 
BEGIN
    SELECT @ErrMsg = 'No Matches are found in this database ' + DB_NAME() + ' for the specified filter'
    PRINT @ErrMsg
    RETURN
END
SET NOCOUNT OFF

0 个答案:

没有答案