如何从SQL Server数据库表中读取元数据?

时间:2014-04-09 14:49:04

标签: sql sql-server database metadata

我是SQL Server的初学者。我得到了一个SQL Server备份文件格式的数据库。我想出了如何恢复数据库,但现在我想要导出表(最终到Stata .dta文件)

我很困惑如何查看和提取我的SQL Server数据库可能包含的任何元数据。例如,我有一个标记为Sex的列,值为1和2.但是,我不知道哪个数字指男性,哪个指女性。如何查看列描述(如果存在)以查看是否有任何标签可能能够澄清此问题?

编辑:快速提问。如果我使用导入/导出向导,那会自动提取元数据吗?

3 个答案:

答案 0 :(得分:2)

这是迄今为止从SQL导出到excel的最佳帖子:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=49926

要使用标题(列名称)将数据导出到新的EXCEL文件,请创建以下过程

create procedure proc_generate_excel_with_columns
(
    @db_name    varchar(100),
    @table_name varchar(100),   
    @file_name  varchar(100)
)
as

--Generate column names as a recordset
declare @columns varchar(8000), @sql varchar(8000), @data_file varchar(100)
select 
    @columns=coalesce(@columns+',','')+column_name+' as '+column_name 
from 
    information_schema.columns
where 
    table_name=@table_name
select @columns=''''''+replace(replace(@columns,' as ',''''' as '),',',',''''')

--Create a dummy file to have actual data
select @data_file=substring(@file_name,1,len(@file_name)-charindex('\',reverse(@file_name)))+'\data_file.xls'

--Generate column names in the passed EXCEL file
set @sql='exec master..xp_cmdshell ''bcp " select * from (select '+@columns+') as t" queryout "'+@file_name+'" -c'''
exec(@sql)

--Generate data in the dummy file
set @sql='exec master..xp_cmdshell ''bcp "select * from '+@db_name+'..'+@table_name+'" queryout "'+@data_file+'" -c'''
exec(@sql)

--Copy dummy file to passed EXCEL file
set @sql= 'exec master..xp_cmdshell ''type '+@data_file+' >> "'+@file_name+'"'''
exec(@sql)

--Delete dummy file 
set @sql= 'exec master..xp_cmdshell ''del '+@data_file+''''
exec(@sql)

创建过程后,通过提供数据库名称,表名和文件路径

来执行它
EXEC proc_generate_excel_with_columns 'your dbname', 'your table name','your file path'

答案 1 :(得分:0)

如果您的数据库管理员和/或开发人员使用了所谓的扩展属性来记录数据库表,那么您可以使用此T-SQL语句检索这些描述:

SELECT 
    TableName = t.Name,
    ColumnName = c.Name,
    ep.class_desc,
    ep.value
FROM 
    sys.extended_properties ep
INNER JOIN 
    sys.tables t ON ep.major_id = t.object_id
INNER JOIN 
    sys.columns c ON ep.major_id = c.object_id AND ep.minor_id = c.column_id

它使用一些SQL Server 目录视图sys.tables等),其中包含有关SQL Server数据库对象的所有有价值的元数据信息。

答案 2 :(得分:0)

假设您有正确的外键,您可以搜索使用以下T-SQL引用的列:

SELECT constraint_object_name
     , constraint_object_name
     , referenced_object_name
     , referenced_column_name
FROM   (
        SELECT Object_Name(parent_object_id) As constraint_object_name
             , constraint_columns.name As constraint_column_name
             , Object_Name(referenced_object_id) As referenced_object_name
             , referenced_columns.name As referenced_column_name
        FROM   sys.foreign_key_columns
         INNER
          JOIN sys.columns As constraint_columns
            ON constraint_columns.object_id = foreign_key_columns.parent_object_id
           AND constraint_columns.column_id = foreign_key_columns.parent_column_id
         INNER
          JOIN sys.columns As referenced_columns
            ON referenced_columns.object_id = foreign_key_columns.referenced_object_id
           AND referenced_columns.column_id = foreign_key_columns.referenced_column_id
       ) As foreign_key_details
WHERE  'SearchingForThisColumnHere' IN (constraint_column_name, referenced_column_name)

只需搜索共享名称的列

SELECT Object_Name(object_id) As object_name
     , name As column_name
FROM   sys.columns
WHERE  name = 'SearchingForThisColumnHere'