October 12, 2007 at 10:19 pm
Comments posted to this topic are about the item Truncating and deleting all tables in a database
April 11, 2008 at 10:07 am
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.
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 + ']'
Before someone says "You shouldn't put such characters in table names", the table names are not under my control.
May 6, 2009 at 1:18 pm
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
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply