﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by hunchback  / Truncating and deleting all tables in a database / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Thu, 23 May 2013 16:51:33 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Truncating and deleting all tables in a database</title><link>http://www.sqlservercentral.com/Forums/Topic410257-901-1.aspx</link><description>Hi,I came across undocumented system stored procedure sp_MSForEachTable.Try this to delete all data from all tables in a given database. Please Test it.EXEC sp_MSForEachTable ‘ALTER TABLE ? NOCHECK CONSTRAINT ALL’EXEC sp_MSForEachTable ‘DELETE FROM ?’EXEC sp_MSForEachTable ‘ALTER TABLE ? CHECK CONSTRAINT ALL’Thanks ,Prem</description><pubDate>Wed, 06 May 2009 13:18:04 GMT</pubDate><dc:creator>prem</dc:creator></item><item><title>RE: Truncating and deleting all tables in a database</title><link>http://www.sqlservercentral.com/Forums/Topic410257-901-1.aspx</link><description>I'm no SQL expert so correct me if I'm wrong, but...This script gives me syntax errors because some of the table names in my database include spaces, hyphens and other strange characters.So shouldn't we put brackets around the table names, i.e.[code]	if exists (select * from sysreferences where rkeyid = object_id(@tn))                /*table referenced by a FOREIGN KEY constraint */  		set @sql = 'delete [' + @tn + ']'	else		set @sql = 'truncate table [' + @tn + ']'[/code]Before someone says "You shouldn't put such characters in table names", the table names are not under my control.</description><pubDate>Fri, 11 Apr 2008 10:07:23 GMT</pubDate><dc:creator>Lars Huttar</dc:creator></item><item><title>Truncating and deleting all tables in a database</title><link>http://www.sqlservercentral.com/Forums/Topic410257-901-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/scripts/Miscellaneous/30274/"&gt;Truncating and deleting all tables in a database&lt;/A&gt;[/B]</description><pubDate>Fri, 12 Oct 2007 22:19:47 GMT</pubDate><dc:creator>hunchback</dc:creator></item></channel></rss>