简体   繁体   English

搜索数据库中的所有表并返回值?

[英]Search all tables in database and return a value?

I'm using SQL Server 2008 and working off of the follow example HERE . 我使用的是SQL Server 2008和关闭,按照实施例的工作这里

Below is the code I have so far. 下面是我到目前为止的代码。 (The only difference is a turned it into a query from a stored procedure and removed schema name which wasn't necessary.) (唯一的区别是将其转换为来自存储过程的查询,并删除了不必要的模式名称。)

What I'd like to do is add another column to the result and give the first column's data value. 我想做的是在结果中添加另一列,并提供第一列的数据值。 In other words if each table starts off with a Primary Key, my result will include the Primary Key of the row where the data was found. 换句话说,如果每个表都以主键开始,那么我的结果将包括找到数据所在行的主键。 Is this even possible? 这有可能吗? Thanks. 谢谢。

DECLARE @DataToFind nvarchar(MAX) = ''
DECLARE @ExactMatch BIT = 0

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  TableName, ColumnName
FROM    @Temp
WHERE   DataFound = 1

Returning the primary key is absolutely possible. 绝对有可能返回主键。 You will need to: 您将需要:

  • Determine the name if the primary key column or columns in each table. 确定名称是否是每个表中的一个或多个主键列。 If you can stick to a single column, it will make your life much easier. 如果您可以坚持使用单列,那么将使您的生活更加轻松。
  • Add a variable for the content of the Primary Key value, say @PrimaryKeyValue 为主键值的内容添加一个变量,例如@PrimaryKeyValue
  • Modify @SQLTemplate to return the value of this column into @PrimaryKeyValue , something like this: 修改@SQLTemplate以将此列的值返回到@PrimaryKeyValue ,如下所示:

     SELECT @SQLTemplate = CASE WHEN @ExactMatch = 1 THEN 'select @PrimaryKeyValue = min(' + @PrimaryKeyColumnName + ') from ReplaceTableName where Convert(nVarChar(4000), [ReplaceColumnName]) = ''' + @DataToFind + ''')' ELSE 'select @PrimaryKeyValue = min(' + @PrimaryKeyColumnName + ') from ReplaceTableName where Convert(nVarChar(4000), [ReplaceColumnName]) Like ''%' + @DataToFind + '%'')' END, @PARAMETERS = '@PrimaryKeyValue nvarchar(4000) OUTPUT', @i = 1 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM