{"id":190,"date":"2011-04-11T18:37:00","date_gmt":"2011-04-11T18:37:00","guid":{"rendered":"http:\/\/turtlellc.com\/index.php\/clearing-all-rows-from-all-tables\/"},"modified":"2011-04-11T18:37:00","modified_gmt":"2011-04-11T18:37:00","slug":"clearing-all-rows-from-all-tables","status":"publish","type":"post","link":"http:\/\/www.turtle.works\/knowledge\/clearing-all-rows-from-all-tables\/","title":{"rendered":"Clearing All Rows From All Tables"},"content":{"rendered":"<p><span style=\"font-family:trebuchet ms;\">This couple of SQL statements will delete all the rows from all tables.<\/span><\/p>\n<ol style=\"font-family: trebuchet ms;\">\n<li>It disables referential integrity<\/li>\n<li>DELETES or TRUNCATES each table<\/li>\n<li>Enables referential integrity<\/li>\n<li>Reseeds rows with identity<\/li>\n<\/ol>\n<p><span style=\"font-family:trebuchet ms;\">&#8212; disable referential integrity<\/span><br \/><span style=\"font-family:trebuchet ms;\">EXEC sp_MSForEachTable &#8216;ALTER TABLE ? NOCHECK CONSTRAINT ALL&#8217;<\/span><br \/><span style=\"font-family:trebuchet ms;\">GO<\/span><\/p>\n<p><span style=\"font-family:trebuchet ms;\">EXEC sp_MSForEachTable &#8216;<\/span><br \/><span style=\"font-family:trebuchet ms;\"> IF OBJECTPROPERTY(object_id(&#8221;?&#8221;), &#8221;TableHasForeignRef&#8221;) = 1<\/span><br \/><span style=\"font-family:trebuchet ms;\">  DELETE FROM ?<\/span><br \/><span style=\"font-family:trebuchet ms;\"> else<\/span><br \/><span style=\"font-family:trebuchet ms;\">  TRUNCATE TABLE ?<\/span><br \/><span style=\"font-family:trebuchet ms;\">&#8216;<\/span><br \/><span style=\"font-family:trebuchet ms;\">GO<\/span><\/p>\n<p><span style=\"font-family:trebuchet ms;\">&#8212; enable referential integrity again<\/span><br \/><span style=\"font-family:trebuchet ms;\">EXEC sp_MSForEachTable &#8216;ALTER TABLE ? CHECK CONSTRAINT ALL&#8217;<\/span><br \/><span style=\"font-family:trebuchet ms;\">GO<\/span><\/p>\n<p><span style=\"font-family:trebuchet ms;\">&#8212; This will reseed each table [don&#8217;t run this exec if you don&#8217;t want all your seeds to be reset]<\/span><br \/><span style=\"font-family:trebuchet ms;\">EXEC sp_MSForEachTable &#8216;<\/span><br \/><span style=\"font-family:trebuchet ms;\">IF OBJECTPROPERTY(object_id(&#8221;?&#8221;), &#8221;TableHasIdentity&#8221;) = 1<\/span><br \/><span style=\"font-family:trebuchet ms;\">DBCC CHECKIDENT (&#8221;?&#8221;, RESEED, 0)<\/span><br \/><span style=\"font-family:trebuchet ms;\">&#8216;<\/span><br \/><span style=\"font-family:trebuchet ms;\">GO <\/span><\/p>\n<p><span style=\"font-family:trebuchet ms;\">Thanks to Mauro Cardarelli&#8217;s <\/span><a style=\"font-family: trebuchet ms;\" href=\"http:\/\/blogs.officezealot.com\/mauro\/archive\/2006\/03\/12\/9402.aspx\">post<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This couple of SQL statements will delete all the rows from all tables. It disables referential integrity DELETES or TRUNCATES each table Enables referential integrity Reseeds rows with identity &#8212; disable referential integrityEXEC sp_MSForEachTable &#8216;ALTER TABLE ? NOCHECK CONSTRAINT ALL&#8217;GO EXEC sp_MSForEachTable &#8216; IF OBJECTPROPERTY(object_id(&#8221;?&#8221;), &#8221;TableHasForeignRef&#8221;) = 1 DELETE FROM ? else TRUNCATE TABLE ?&#8216;GO &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/www.turtle.works\/knowledge\/clearing-all-rows-from-all-tables\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Clearing All Rows From All Tables&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[9,25,26,28],"tags":[],"class_list":["post-190","post","type-post","status-publish","format-standard","hentry","category-database","category-sql","category-sql-server","category-t-sql"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Clearing All Rows From All Tables - Turtle Works<\/title>\n<meta name=\"description\" content=\"A knowledge base written by Turtle Works LLC to help you achieve Clearing All Rows From All Tables - Turtle Works\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.turtle.works\/knowledge\/clearing-all-rows-from-all-tables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Clearing All Rows From All Tables - Turtle Works\" \/>\n<meta property=\"og:description\" content=\"A knowledge base written by Turtle Works LLC to help you achieve Clearing All Rows From All Tables - Turtle Works\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.turtle.works\/knowledge\/clearing-all-rows-from-all-tables\/\" \/>\n<meta property=\"og:site_name\" content=\"Turtle Works\" \/>\n<meta property=\"article:published_time\" content=\"2011-04-11T18:37:00+00:00\" \/>\n<meta name=\"author\" content=\"josef.wainz\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"josef.wainz\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\\\/\\\/www.turtle.works\\\/knowledge\\\/clearing-all-rows-from-all-tables\\\/#article\",\"isPartOf\":{\"@id\":\"http:\\\/\\\/www.turtle.works\\\/knowledge\\\/clearing-all-rows-from-all-tables\\\/\"},\"author\":{\"name\":\"josef.wainz\",\"@id\":\"https:\\\/\\\/www.turtle.works\\\/knowledge\\\/#\\\/schema\\\/person\\\/cb79278290029b781485a7b61b9b9a0b\"},\"headline\":\"Clearing All Rows From All Tables\",\"datePublished\":\"2011-04-11T18:37:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\\\/\\\/www.turtle.works\\\/knowledge\\\/clearing-all-rows-from-all-tables\\\/\"},\"wordCount\":116,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.turtle.works\\\/knowledge\\\/#organization\"},\"articleSection\":[\"Database\",\"SQL\",\"SQL Server\",\"T-SQL\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\\\/\\\/www.turtle.works\\\/knowledge\\\/clearing-all-rows-from-all-tables\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"http:\\\/\\\/www.turtle.works\\\/knowledge\\\/clearing-all-rows-from-all-tables\\\/\",\"url\":\"http:\\\/\\\/www.turtle.works\\\/knowledge\\\/clearing-all-rows-from-all-tables\\\/\",\"name\":\"Clearing All Rows From All Tables - Turtle Works\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.turtle.works\\\/knowledge\\\/#website\"},\"datePublished\":\"2011-04-11T18:37:00+00:00\",\"description\":\"A knowledge base written by Turtle Works LLC to help you achieve Clearing All Rows From All Tables - Turtle Works\",\"breadcrumb\":{\"@id\":\"http:\\\/\\\/www.turtle.works\\\/knowledge\\\/clearing-all-rows-from-all-tables\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\\\/\\\/www.turtle.works\\\/knowledge\\\/clearing-all-rows-from-all-tables\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\\\/\\\/www.turtle.works\\\/knowledge\\\/clearing-all-rows-from-all-tables\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.turtle.works\\\/knowledge\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Clearing All Rows From All Tables\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.turtle.works\\\/knowledge\\\/#website\",\"url\":\"https:\\\/\\\/www.turtle.works\\\/knowledge\\\/\",\"name\":\"Turtle Works\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.turtle.works\\\/knowledge\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.turtle.works\\\/knowledge\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.turtle.works\\\/knowledge\\\/#organization\",\"name\":\"Turtle Works\",\"url\":\"https:\\\/\\\/www.turtle.works\\\/knowledge\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.turtle.works\\\/knowledge\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"http:\\\/\\\/www.turtle.works\\\/knowledge\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/cropped-TurtleWorks.png\",\"contentUrl\":\"http:\\\/\\\/www.turtle.works\\\/knowledge\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/cropped-TurtleWorks.png\",\"width\":625,\"height\":250,\"caption\":\"Turtle Works\"},\"image\":{\"@id\":\"https:\\\/\\\/www.turtle.works\\\/knowledge\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.turtle.works\\\/knowledge\\\/#\\\/schema\\\/person\\\/cb79278290029b781485a7b61b9b9a0b\",\"name\":\"josef.wainz\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a7c48ebcd2ee23a7db62291036c5ff0cd06cf9f7595838f86ecb17784c9af59c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a7c48ebcd2ee23a7db62291036c5ff0cd06cf9f7595838f86ecb17784c9af59c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a7c48ebcd2ee23a7db62291036c5ff0cd06cf9f7595838f86ecb17784c9af59c?s=96&d=mm&r=g\",\"caption\":\"josef.wainz\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Clearing All Rows From All Tables - Turtle Works","description":"A knowledge base written by Turtle Works LLC to help you achieve Clearing All Rows From All Tables - Turtle Works","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/www.turtle.works\/knowledge\/clearing-all-rows-from-all-tables\/","og_locale":"en_US","og_type":"article","og_title":"Clearing All Rows From All Tables - Turtle Works","og_description":"A knowledge base written by Turtle Works LLC to help you achieve Clearing All Rows From All Tables - Turtle Works","og_url":"http:\/\/www.turtle.works\/knowledge\/clearing-all-rows-from-all-tables\/","og_site_name":"Turtle Works","article_published_time":"2011-04-11T18:37:00+00:00","author":"josef.wainz","twitter_card":"summary_large_image","twitter_misc":{"Written by":"josef.wainz","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/www.turtle.works\/knowledge\/clearing-all-rows-from-all-tables\/#article","isPartOf":{"@id":"http:\/\/www.turtle.works\/knowledge\/clearing-all-rows-from-all-tables\/"},"author":{"name":"josef.wainz","@id":"https:\/\/www.turtle.works\/knowledge\/#\/schema\/person\/cb79278290029b781485a7b61b9b9a0b"},"headline":"Clearing All Rows From All Tables","datePublished":"2011-04-11T18:37:00+00:00","mainEntityOfPage":{"@id":"http:\/\/www.turtle.works\/knowledge\/clearing-all-rows-from-all-tables\/"},"wordCount":116,"commentCount":0,"publisher":{"@id":"https:\/\/www.turtle.works\/knowledge\/#organization"},"articleSection":["Database","SQL","SQL Server","T-SQL"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/www.turtle.works\/knowledge\/clearing-all-rows-from-all-tables\/#respond"]}]},{"@type":"WebPage","@id":"http:\/\/www.turtle.works\/knowledge\/clearing-all-rows-from-all-tables\/","url":"http:\/\/www.turtle.works\/knowledge\/clearing-all-rows-from-all-tables\/","name":"Clearing All Rows From All Tables - Turtle Works","isPartOf":{"@id":"https:\/\/www.turtle.works\/knowledge\/#website"},"datePublished":"2011-04-11T18:37:00+00:00","description":"A knowledge base written by Turtle Works LLC to help you achieve Clearing All Rows From All Tables - Turtle Works","breadcrumb":{"@id":"http:\/\/www.turtle.works\/knowledge\/clearing-all-rows-from-all-tables\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.turtle.works\/knowledge\/clearing-all-rows-from-all-tables\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/www.turtle.works\/knowledge\/clearing-all-rows-from-all-tables\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.turtle.works\/knowledge\/"},{"@type":"ListItem","position":2,"name":"Clearing All Rows From All Tables"}]},{"@type":"WebSite","@id":"https:\/\/www.turtle.works\/knowledge\/#website","url":"https:\/\/www.turtle.works\/knowledge\/","name":"Turtle Works","description":"","publisher":{"@id":"https:\/\/www.turtle.works\/knowledge\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.turtle.works\/knowledge\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.turtle.works\/knowledge\/#organization","name":"Turtle Works","url":"https:\/\/www.turtle.works\/knowledge\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.turtle.works\/knowledge\/#\/schema\/logo\/image\/","url":"http:\/\/www.turtle.works\/knowledge\/wp-content\/uploads\/2020\/07\/cropped-TurtleWorks.png","contentUrl":"http:\/\/www.turtle.works\/knowledge\/wp-content\/uploads\/2020\/07\/cropped-TurtleWorks.png","width":625,"height":250,"caption":"Turtle Works"},"image":{"@id":"https:\/\/www.turtle.works\/knowledge\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.turtle.works\/knowledge\/#\/schema\/person\/cb79278290029b781485a7b61b9b9a0b","name":"josef.wainz","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/a7c48ebcd2ee23a7db62291036c5ff0cd06cf9f7595838f86ecb17784c9af59c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/a7c48ebcd2ee23a7db62291036c5ff0cd06cf9f7595838f86ecb17784c9af59c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a7c48ebcd2ee23a7db62291036c5ff0cd06cf9f7595838f86ecb17784c9af59c?s=96&d=mm&r=g","caption":"josef.wainz"}}]}},"_links":{"self":[{"href":"http:\/\/www.turtle.works\/knowledge\/wp-json\/wp\/v2\/posts\/190","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.turtle.works\/knowledge\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.turtle.works\/knowledge\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.turtle.works\/knowledge\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.turtle.works\/knowledge\/wp-json\/wp\/v2\/comments?post=190"}],"version-history":[{"count":0,"href":"http:\/\/www.turtle.works\/knowledge\/wp-json\/wp\/v2\/posts\/190\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.turtle.works\/knowledge\/wp-json\/wp\/v2\/media?parent=190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.turtle.works\/knowledge\/wp-json\/wp\/v2\/categories?post=190"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.turtle.works\/knowledge\/wp-json\/wp\/v2\/tags?post=190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}