|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, January 19, 2012 1:42 PM
Points: 4,
Visits: 32
|
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Monday, August 09, 2010 2:37 PM
Points: 7,
Visits: 26
|
|
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.
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Wednesday, March 02, 2011 10:19 AM
Points: 320,
Visits: 160
|
|
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
|
|
|
|