0

我想知道是否可以在整个 sql 数据库中搜索一串数据?

我知道您可以搜索某些列标题等,但我需要搜索单个数据字符串并且不知道它将保存在数据库中的哪个位置(表/列等)。

可能吗?我将如何执行此操作以返回其所在位置的列、表或两者?

非常感谢任何建议。

(我使用的是 sql server management studio 2005)

4

2 回答 2

0

您可以为此编写一个脚本

select *, COLUMN_NAME, TABLE_NAME 
from INFORMATION_SCHEMA.COLUMNS
where TABLE_SCHEMA = 'your_schema'

使用该表和列列表,您可以遍历所有表和列以查找您的输入。

于 2012-05-22T15:32:45.203 回答
0

这是我最后使用的脚本。非常直截了当,还有一件很棒的小事要知道。

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 'VBAR2', 0 
于 2012-05-23T13:12:48.790 回答