开发者社区> 问答> 正文

如何搜索 SQL Server 数据库以查找字符串?

如何搜索 SQL Server 数据库以查找字符串?

展开
收起
贺贺_ 2019-12-02 21:32:26 562 0
1 条回答
写回答
取消 提交回答
  • 这将搜索特定数据库中每个表的每一列。在要搜索的数据库上创建存储过程。

    CREATE PROCEDURE FindMyData_String
        @DataToFind NVARCHAR(4000),
        @ExactMatch BIT = 0
    AS
    SET NOCOUNT ON
    
    DECLARE @Temp TABLE(RowId INT IDENTITY(1,1), SchemaName sysname, TableName sysname, ColumnName SysName, DataType VARCHAR(100), DataFound BIT)
    
        INSERT  INTO @Temp(TableName,SchemaName, ColumnName, DataType)
        SELECT  C.Table_Name,C.TABLE_SCHEMA, C.Column_Name, C.Data_Type
        FROM    Information_Schema.Columns AS C
                INNER Join Information_Schema.Tables AS T
                    ON C.Table_Name = T.Table_Name
            AND C.TABLE_SCHEMA = T.TABLE_SCHEMA
        WHERE   Table_Type = 'Base Table'
                And Data_Type In ('ntext','text','nvarchar','nchar','varchar','char')
    
    
    DECLARE @i INT
    DECLARE @MAX INT
    DECLARE @TableName sysname
    DECLARE @ColumnName sysname
    DECLARE @SchemaName sysname
    DECLARE @SQL NVARCHAR(4000)
    DECLARE @PARAMETERS NVARCHAR(4000)
    DECLARE @DataExists BIT
    DECLARE @SQLTemplate NVARCHAR(4000)
    
    SELECT  @SQLTemplate = CASE WHEN @ExactMatch = 1
                                THEN 'If Exists(Select *
                                              From   ReplaceTableName
                                              Where  Convert(nVarChar(4000), [ReplaceColumnName])
                                                           = ''' + @DataToFind + '''
                                              )
                                         Set @DataExists = 1
                                     Else
                                         Set @DataExists = 0'
                                ELSE 'If Exists(Select *
                                              From   ReplaceTableName
                                              Where  Convert(nVarChar(4000), [ReplaceColumnName])
                                                           Like ''%' + @DataToFind + '%''
                                              )
                                         Set @DataExists = 1
                                     Else
                                         Set @DataExists = 0'
                                END,
            @PARAMETERS = '@DataExists Bit OUTPUT',
            @i = 1
    
    SELECT @i = 1, @MAX = MAX(RowId)
    FROM   @Temp
    
    WHILE @i <= @MAX
        BEGIN
            SELECT  @SQL = REPLACE(REPLACE(@SQLTemplate, 'ReplaceTableName', QUOTENAME(SchemaName) + '.' + QUOTENAME(TableName)), 'ReplaceColumnName', ColumnName)
            FROM    @Temp
            WHERE   RowId = @i
    
    
            PRINT @SQL
            EXEC SP_EXECUTESQL @SQL, @PARAMETERS, @DataExists = @DataExists OUTPUT
    
            IF @DataExists =1
                UPDATE @Temp SET DataFound = 1 WHERE RowId = @i
    
            SET @i = @i + 1
        END
    
    SELECT  SchemaName,TableName, ColumnName
    FROM    @Temp
    WHERE   DataFound = 1
    GO
    
    

    要运行它,只需执行此操作:

    exec FindMyData_string 'google', 0
    
    2019-12-02 21:33:25
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
DTCC 2022大会集锦《云原生一站式数据库技术与实践》 立即下载
阿里云瑶池数据库精要2022版 立即下载
2022 DTCC-阿里云一站式数据库上云最佳实践 立即下载