如何查找包含指定列值的表和列

时间:2015-01-04 23:45:11

标签: sql-server tsql

在T-SQL中,是否可以为我提供包含指定列值的表和列的列表?

例如,我从SQL Server数据库生成一个员工报告,该报告在其中一列中显示“John”作为名称。现在我想找出“John”在数据库的任何表/列中作为字段值出现的位置。

所以在英语中我的查询应该听起来像;

Select table, column 
from database A 
where field = 'John'

由于

1 个答案:

答案 0 :(得分:2)

尝试使用此程序

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
  @DataToFind='Enter Your String'