如何在 SQL Server 的数据库中搜索字符串

2022-01-01 00:00:00 database sql-server-2005 sql-server

此查询可以从任何 SQL Server 数据库的所有表/视图中搜索任何字符串值(表名、表数据等),当您将任何字符串放在您的文本此处"时将从该数据库中存在的所有表/视图中搜索给定的字符串,

This Query can search any string value (table name, table data etc) from all the tables/views of any SQL Server database, when you will place any string at "your text here" it will search your given string from all the tables/views exist in that database,

DECLARE @SearchStr nvarchar(100) 
    SET @SearchStr = 'your text here'

DECLARE @Results TABLE(ColumnName nvarchar(370), ColumnValue nvarchar(3630))

SET NOCOUNT ON

DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110)
SET  @TableName = ''
SET @SearchStr2 = QUOTENAME('%' + @SearchStr + '%','''')

WHILE @TableName IS NOT NULL
BEGIN
    SET @ColumnName = ''
    SET @TableName = 
    (
        SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
        FROM    INFORMATION_SCHEMA.TABLES
        WHERE       TABLE_TYPE = 'BASE TABLE'
            AND QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
            AND OBJECTPROPERTY(
                    OBJECT_ID(
                        QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
                         ), 'IsMSShipped'
                           ) = 0
    )

    WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)
    BEGIN
        SET @ColumnName =
        (
            SELECT MIN(QUOTENAME(COLUMN_NAME))
            FROM    INFORMATION_SCHEMA.COLUMNS
            WHERE       TABLE_SCHEMA    = PARSENAME(@TableName, 2)
                AND TABLE_NAME  = PARSENAME(@TableName, 1)
                AND DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar')
                AND QUOTENAME(COLUMN_NAME) > @ColumnName
        )

        IF @ColumnName IS NOT NULL
        BEGIN
            INSERT INTO @Results
            EXEC
            (
                'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630) 
                FROM ' + @TableName + ' (NOLOCK) ' +
                ' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2
            )
        END
    END 
END

SELECT ColumnName, ColumnValue FROM @Results

但我喜欢在 procedures/functions/triggers 中搜索给定的字符串以及 SQL Server 2005 中是否存在任何查询.因为此查询不搜索这些领域.

But I like to search given string in the procedures/functions/triggers as well is there any query exist in SQL Server 2005. As this query does not search in these areas.

推荐答案

在过程/函数/触发器中搜索给定的字符串

search given string in the procedures/functions/triggers

这实际上要容易得多.

SELECT OBJECT_NAME(object_id), definition
  FROM sys.sql_modules
 WHERE definition LIKE '%'+@SearchStr+'%'

使用它的一种方法是将其添加到 TSQL 代码的末尾,即修改最后一个 SELECT:

One way to use it is to add it to the end of your TSQL code, i.e modify the last SELECT:

SELECT ColumnName, ColumnValue
  FROM @Results
 UNION ALL
SELECT OBJECT_NAME(object_id), definition
  FROM sys.sql_modules
 WHERE definition LIKE '%'+@SearchStr+'%'

就我个人而言,我只是将它们作为单独的语句一个接一个地单独运行.

Personally, I'd just run them separately one after the other as separate statements.

相关文章