动态重命名索引

时间:2017-10-10 20:01:39

标签: sql-server indexing concatenation rename

我试图做这样的事情(替换' - '用' _'在IndexName上),但不知道如何实现正确的连接

int

任何帮助将不胜感激。 感谢

1 个答案:

答案 0 :(得分:0)

这对你有用。请注意,您将收到Caution消息。 运行此脚本后Caution: Changing any part of an object name could break scripts and stored procedures.

DECLARE @TestTable varchar(50) ='TableTest'

DECLARE @TestDatabase varchar(30) ='DatabaseTest'
Declare @NewTestIndex varchar(150)
DECLARE @TestIndex VARCHAR(150) ='NonClusteredIndex-20150617-075305'
Set @NewTestIndex = Replace(@TestIndex,'-','_')

DECLARE @TestSchema VARCHAR(20) ='SChemaTest'
Declare @cmd varchar(max) 
Set @cmd = 'EXEC sp_rename N''[' + @TestSchema + '].['+@TestTable+'].['+ @TestIndex +']'', N''' +@NewTestIndex + ''', N''INDEX'''

Select @cmd

Execute (@cmd)