我正在开发一个页面,我们需要在两个数据库之间直观地比较同一个表的模式 - 一个在sql server中,另一个在mysql中。我也必须包含索引。
现在mysql查询显示信息以及索引 -
select column_name,column_type,table_name,column_key
from INFORMATION_SCHEMA.COLUMNS where table_name = 'tbl_ClientDN'
但是对于sql server,同一个查询不会返回索引 -
select * from INFORMATION_SCHEMA.COLUMNS where table_name = 'tbl_ClientDN'
所以我需要查询来计算结果 -
sp_helpindex 'tbl_ClientDN'
如何让column_key在mssql查询中显示索引。 有什么建议吗?
答案 0 :(得分:8)
远离INFORMATION_SCHEMA.COLUMNS
,特别是索引,因为过滤索引和包含列之类的东西不是定义的一部分。我在这里更详细地讨论这个问题:
The case against INFORMATION_SCHEMA views
您想使用sys.indexes
和sys.index_columns
。例如:
DECLARE @tablename NVARCHAR(512) = 'dbo.tbl_ClientDN';
SELECT
[Index] = i.name,
[Column] = c.Name,
[Type] = i.type_desc,
PK = i.is_primary_key,
[Unique] = i.is_unique,
[Unique Constraint] = i.is_unique_constraint,
[DESC] = ic.is_descending_key,
[INCLUDE] = ic.is_included_column,
[Filtered] = i.filter_definition -- only for SQL Server 2008+
FROM
sys.indexes AS i
INNER JOIN
sys.index_columns AS ic
ON i.[object_id] = ic.[object_id]
AND i.index_id = ic.index_id
INNER JOIN
sys.columns c
ON ic.column_id = c.column_id
AND ic.[object_id] = c.[object_id]
WHERE
i.[object_id] = OBJECT_ID(@tablename)
ORDER BY [Index], ic.index_column_id;
如果您想一次为所有表执行此操作,那么只需进行简单的更改:
SELECT
[Table] = QUOTENAME(OBJECT_SCHEMA_NAME(i.[object_id]))
+ '.' + QUOTENAME(OBJECT_NAME(i.[object_id])),
[Index] = i.name,
[Column] = c.Name,
[Type] = i.type_desc,
PK = i.is_primary_key,
[Unique] = i.is_unique,
[Unique Constraint] = i.is_unique_constraint,
[DESC] = ic.is_descending_key,
[INCLUDE] = ic.is_included_column,
[Filtered] = i.filter_definition -- only for SQL Server 2008+
FROM
sys.indexes AS i
INNER JOIN
sys.index_columns AS ic
ON i.[object_id] = ic.[object_id]
AND i.index_id = ic.index_id
INNER JOIN
sys.columns c
ON ic.column_id = c.column_id
AND ic.[object_id] = c.[object_id]
ORDER BY [Table], [Index], ic.index_column_id;
您没有指定所使用的SQL Server版本,因此以下是指向sys.indexes
和sys.index_columns
主题的所有链接:
<强> SYS.INDEXES 强>
SQL Server 2005 http://technet.microsoft.com/en-us/library/ms173760%28SQL.90%29.aspx
SQL Server 2008 http://technet.microsoft.com/en-us/library/ms173760%28SQL.100%29.aspx
SQL Server 2008 R2 http://technet.microsoft.com/en-us/library/ms173760%28SQL.105%29.aspx
SQL Server 2012 http://technet.microsoft.com/en-us/library/ms173760%28SQL.110%29.aspx
<强> sys.index_columns 强>
SQL Server 2005 http://technet.microsoft.com/en-us/library/ms175105%28SQL.90%29.aspx
SQL Server 2008 http://technet.microsoft.com/en-us/library/ms175105%28SQL.100%29.aspx
SQL Server 2008 R2 http://technet.microsoft.com/en-us/library/ms175105%28SQL.105%29.aspx
SQL Server 2012 http://technet.microsoft.com/en-us/library/ms175105%28SQL.110%29.aspx
您也可以查看一下Kimberley L. Tripp的sp_helpindex2。
修改强>
总的来说,我同意@ BrianWhite的评论。如果您正在为此付出任何努力,那么您应该使用一个工具来代替重新发明轮子并尝试自己编写。对这个问题进行疑难解答,就时间而言,您可能已经花费了一个好工具的成本。请阅读这篇文章:
http://bertrandaaron.wordpress.com/2012/04/20/re-blog-the-cost-of-reinventing-the-wheel/
答案 1 :(得分:0)
也许你在sp_help
http://msdn.microsoft.com/en-us/library/ms187335.aspx
只需输入
sp_help myTableName
应该为您提供所需的所有数据。