那么,如何使用一个命令删除sql数据库中的所有索引?我有这个命令,将得到我所有的20个左右的drop语句,但是如何从这个“结果集”运行所有这些drop语句?
select * from vw_drop_idnex;
给我相同列表的另一个变体是:
SELECT 'DROP INDEX ' + ix.Name + ' ON ' + OBJECT_NAME(ID) AS QUERYLIST FROM sysindexes ix WHERE ix.Name IS NOT null and ix.Name like '%pre_%'
我试图做“exec(从vw_drop_idnex选择cmd)”,它没有工作.我正在寻找一种类似于for循环的东西,并逐个运行查询.
———————–
有了Rob Farleys的帮助,剧本的最终草案是:
declare @ltr nvarchar(1024); SELECT @ltr = ( select 'alter table '+o.name+' drop constraint '+i.name+';' from sys.indexes i join sys.objects o on i.object_id=o.object_id where o.type<>'S' and is_primary_key=1 FOR xml path('') ); exec sp_executesql @ltr; declare @qry nvarchar(1024); select @qry = (select 'drop index '+o.name+'.'+i.name+';' from sys.indexes i join sys.objects o on i.object_id=o.object_id where o.type<>'S' and is_primary_key<>1 and index_id>0 for xml path('')); exec sp_executesql @qry
解决方法
你很近
declare @qry nvarchar(max); select @qry = (SELECT 'DROP INDEX ' + ix.name + ' ON ' + OBJECT_NAME(ID) + '; ' FROM sysindexes ix WHERE ix.Name IS NOT null and ix.Name like '%prefix_%' for xml path('')); exec sp_executesql @qry