title: What is the fastest way to clear a SQL table?
body: <p>I have a table with about 300,000 rows and 30 columns. How can I quickly clear it out? If I do a <code>DROP FROM MyTable</code> query, it takes a long time to run. I'm trying the following stored procedure to basically make a copy of the table with no data, drop the original table, and rename the new table as the original:</p>
  
  <pre><code>USE [myDatabase]
  GO
  SET ANSI_NULLS_ON
  GO
  SET QUOTED_IDENTIFIER ON
  GO
  ALTER PROCEDURE [dbo].[ClearTheTable]
  AS
  BEGIN
      SET NOCOUNT ON;
      SELECT * INTO tempMyTable FROM MyTable WHERE 1 = 0;
      DROP TABLE MyTable
      EXEC sp_rename tempMyTable, MyTable;
  END
  </code></pre>
  
  <p>This took nearly 7 minutes to run. Is there a faster way to do it? I don't need any logging, rollback or anything of that nature. </p>
  
  <p>If it matters, I'm calling the stored procedure from a C# app. I guess I could write some code to recreate the table from scratch after doing a <code>DROP TABLE</code>, but I didn't want to have to recompile the app any time a column is added or changed.</p>
  
  <p>Thanks!</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here's my updated stored procedure: </p>
  
  <pre><code>USE [myDatabase]
  GO
  SET ANSI_NULLS_ON
  GO
  SET QUOTED_IDENTIFIER ON
  GO
  ALTER PROCEDURE [dbo].[ClearTheTable]
  AS
  BEGIN
      SET NOCOUNT ON;
      ALTER DATABASE myDatabase
      SET RESTRICTED_USER WITH ROLLBACK IMMEDIATE
      TRUNCATE TABLE MyTable
      ALTER DATABASE myDatabase
      SET MULTI_USER
  END
  </code></pre>