[英]Get name of index for a given table and column with T-SQL
I want to convert some databases from Varchar to nVarchar because of switch to unicode. 我想将某些数据库从Varchar转换为nVarchar,因为切换到了unicode。 For that I have begin to write a small Delphi application to generate the needed SQL-script. 为此,我已经开始编写一个小的Delphi应用程序来生成所需的SQL脚本。
Now to alter column from VARCHAR to NVARCHAR I must first drop index if it exists. 现在要将列从VARCHAR更改为NVARCHAR,我必须首先删除索引(如果存在)。 So my question is how can I get the name of index from any given table and column ? 所以我的问题是如何从任何给定的表和列中获取索引的名称?
Try this query 试试这个查询
SELECT TableName = t.NAME
,IndexName = ind.NAME
,IndexId = ind.index_id
,ColumnId = ic.index_column_id
,ColumnName = col.NAME
,ind.*
,ic.*
,col.*
FROM sys.indexes ind
INNER JOIN sys.index_columns ic ON ind.object_id = ic.object_id
AND ind.index_id = ic.index_id
INNER JOIN sys.columns col ON ic.object_id = col.object_id
AND ic.column_id = col.column_id
INNER JOIN sys.tables t ON ind.object_id = t.object_id
WHERE ind.is_primary_key = 0
AND ind.is_unique = 0
AND ind.is_unique_constraint = 0
AND t.is_ms_shipped = 0
AND t.name=<Your TableName>
ORDER BY t.NAME
,ind.NAME
,ind.index_id
,ic.index_column_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.