SQL Server中获得所有表相关信息的方法

来源:互联网 发布:js在数组中查找字符串 编辑:程序博客网 时间:2024/04/30 13:05
 在SQL Server 2005中有时候我们需要获得表的结构信息。可以通过建立View来获得所有标的相关信息,然后提交应用程序作处理。

代码如下:

select s.name as TABLE_SCHEMA, t.name as TABLE_NAME, k.name as CONSTRAINT_NAME, c.name as COLUMN_NAME, ic.key_ordinal AS ORDINAL_POSITION
  
from sys.key_constraints as k
  
join sys.tables as t
    
on t.object_id = k.parent_object_id
  
join sys.schemas as s
    
on s.schema_id = t.schema_id
  
join sys.index_columns as ic
    
on ic.object_id = t.object_id
   
and ic.index_id = k.unique_index_id
  
join sys.columns as c
    
on c.object_id = t.object_id
   
and c.column_id = ic.column_id
 
where k.type = 'PK';
原创粉丝点击