How can I drop all indexes in a SQL database with one command?

前端 未结 7 1419
温柔的废话
温柔的废话 2021-02-04 00:01

So, how can I drop all indexes in a SQL database with one command? I have this command that will get me all the 20 or so drop statements, but how can I run all of those drop s

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-04 00:42

    You're very close.

    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
    

提交回复
热议问题