Neal Waterstreet

  • Home
  • About
  • Book Reviews
    • 2018 Reading List
    • 2017 Reading List
    • 2016 Reading List
    • 2015 Reading List
    • 2014 Reading List
    • 2013 Reading List
  • Projects
    • OBi200
    • DS716+ memory upgrade
    • Cord cutting
  • Presentations
  • Contact

Archives for October 10, 2011

Delete all rows in database

October 10, 2011 by neal Leave a Comment

I recently ran into a situation where I had to truncate an entire database. Usually not a good sign, but this was to clean out a archive/logging database for someone to practice on. The script below works well and is based on sp_ms_foreachtable, which is unsupported but very useful.

--First, disable all constraints on all tables.
EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT ALL"
--Second, delete the data from all tables.
EXEC sp_MSForEachTable "DELETE FROM ?"
--Third, re -enable all of the constraints
EXEC sp_msforeachtable "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT ALL"
--Optionally, you can use DBCC CHECKIDENT to reseed all of the tables staring at 0 or whatever you want.
EXEC sp_MSforeachtable "DBCC CHECKIDENT ( '?', RESEED, 0)"

Filed Under: Scripts

October 2011
M T W T F S S
 12
3456789
10111213141516
17181920212223
24252627282930
31  
« Mar   Nov »

Blogroll

  • Atlanta MDF
  • Atlanta Microsoft Business Intelligence Group
  • Atlanta Modern Excel User Group
  • Chris Webb's BI Blog
  • codegumbo
  • Lance England
  • PowerPivotPro
  • Prologika
  • SQLBI

Tags

#SQLSatATLBI Aereo at Atlanta Microsoft BI Azure Azure DataFest Backups Batch Certification changeset Chicago Conference CrashPlan Data Modeling Data Quality DAX Deployment DVR Entity-based staging Error Messages Excel Filter Goals Headphones Healthcare Leaf Tables MDM MDS MDS Add-in for Excel Oops! Something went wrong PASS Healthcare VC Power BI Power Pivot review Roku SQLSaturday SQL Saturday 800 SQLSaturday Atlanta BI SSIS Streaming Subscription View Time Training Validation Version Flag

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Copyright © 2023 · Minimum Pro Theme on Genesis Framework · WordPress · Log in