{"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":"https:\/\/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=\"https:\/\/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":{"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"],"_links":{"self":[{"href":"https:\/\/www.turtle.works\/knowledge\/wp-json\/wp\/v2\/posts\/190","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.turtle.works\/knowledge\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.turtle.works\/knowledge\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.turtle.works\/knowledge\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.turtle.works\/knowledge\/wp-json\/wp\/v2\/comments?post=190"}],"version-history":[{"count":0,"href":"https:\/\/www.turtle.works\/knowledge\/wp-json\/wp\/v2\/posts\/190\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.turtle.works\/knowledge\/wp-json\/wp\/v2\/media?parent=190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.turtle.works\/knowledge\/wp-json\/wp\/v2\/categories?post=190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.turtle.works\/knowledge\/wp-json\/wp\/v2\/tags?post=190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}