在数据库中查找表

时间:2014-03-14 13:53:52

标签: sql sql-server tsql

在Microsoft SQL Server中

我需要找到列名和表名所在的数据库和服务器。

我知道一个表名,列名。

如何找到表和列所在的数据库?

5 个答案:

答案 0 :(得分:4)

您需要遍历所有数据库才能执行此操作:

IF OBJECT_ID('tempdb..#DBNames', 'U') IS NOT NULL
      DROP TABLE #DBNames

    Create Table #DBNames (
        DBName  varchar(256)
    )
    Insert  #DBNames
    Select  name
    From    sys.databases
    Where   owner_sid <> 1

    Declare @DBName nvarchar(256)
            ,@TableName nvarchar(256)
            ,@ColumnName nvarchar(256)
            ,@sql nvarchar(2000)

    Select  @TableName = 'YourTableName'
            ,@ColumnName = 'YourColumnName'

    Declare SearchForDBTable Cursor For
    Select  DBName
    From    #DBNames

    Open SearchForDBTable
    Fetch Next From SearchForDBTable Into @DBName

    While @@FETCH_STATUS = 0
    Begin
        Set @sql = 'Select  @DBName DBName
                            ,t.name TableName
                            ,c.name ColumnName
                    From    sys.tables t
                    join    sys.columns c
                        on  t.object_id = c.object_id
                    Where   t.name = @TableName
                        And c.name = @ColumnName'
        exec sp_executesql  @sql
                            ,N'@DBName varchar(256), @TableName varchar(256), @ColumnName varchar(256)'
                            ,@DBName = @DBName
                            ,@TableName = @TableName
                            ,@ColumnName = @ColumnName

    Fetch Next From SearchForDBTable Into @DBName
    End

    Close SearchForDBTable
    Deallocate SearchForDBTable

答案 1 :(得分:2)

指定Just Column Name

SELECT t.name AS table_name, SCHEMA_NAME(schema_id) AS schema_name,
 c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name LIKE '%Column_Name%'
ORDER BY schema_name, table_name;

答案 2 :(得分:2)

在SqlServer中,如果存在表和列

,则此查询将返回一行
select * from sysobjects T
JOIN syscolumns C ON T.id = C.id
where c.name = 'COLUMNNAME' and t.name = 'TABLENAME'

答案 3 :(得分:2)

这是一种方法。如果tabe.column存在于当前“选定”的数据库中,则从查询开始确定:

SELECT db_name()
 from sys.tables ta
  inner join sys.columns co
   on co.object_id = ta.object_id
 where ta.name = 'Users'
  and co.name = 'CreatedOn'

将其转换为仅在找到结果时生成结果的例程:

IF exists (select 1
            from sys.tables ta
             inner join sys.columns co
              on co.object_id = ta.object_id
            where ta.name = 'Users'
             and co.name = 'CreatedOn')
    PRINT db_name()

然后将其插入未记录的但非常有用的系统程序sp_msForEachDB

EXECUTE sp_msForEachDB '
USE ?  --  Change context to "this" database

IF exists (select 1
            from sys.tables ta
             inner join sys.columns co
              on co.object_id = ta.object_id
            where ta.name = ''Users''
             and co.name = ''CreatedOn'')
    PRINT db_name()
'

这将打印出包含此表和列的实例上每个数据库的名称。

答案 4 :(得分:2)

这是使用INFORMATION_SCHEMA执行此操作的另一种方法。您可以扩展select语句中的内容,但裸骨如下。自从SQL Server 2005以来,我一直在使用类似于此的脚本,而这只是使用SQL Server 2012编写的。

DECLARE @somecolumn VARCHAR(155) = 'name'

SELECT t.TABLE_CATALOG, t.TABLE_NAME, c.Column_name, c.ORDINAL_Position
FROM INFORMATION_SCHEMA.TABLES t
INNER JOIN INFORMATION_SCHEMA.COLUMNS c
ON t.TABLE_NAME = c.TABLE_NAME AND c.COLUMN_NAME = @somecolumn
WHERE t.TABLE_TYPE = 'BASE TABLE'