title: How can I drop all indexes in a SQL database with one command?
body: <p>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 statements from this "result set"?</p>
  
  <pre><code>select * from vw_drop_idnex;
  </code></pre>
  
  <p>Another variation that gives me the same list is:</p>
  
  <pre><code>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_%'
  </code></pre>
  
  <p>I tried to do "exec(select cmd from vw_drop_idnex)" and it didn't work.  I am looking for something that works like a for loop and runs the queries one by one.</p>
  
  <h2>-----------------------</h2>
  
  <p>With Rob Farleys help, final draft of the script is:</p>
  
  <pre><code>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&lt;&gt;'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&lt;&gt;'S' and is_primary_key&lt;&gt;1 and index_id&gt;0
  for xml path(''));
  exec sp_executesql @qry
  </code></pre>