|
|
Friday, January 04, 2008 #
| Please spread the word if you like this content! |
|
|
|
|
| |
Hi kids... Here's a hot action script for truncating all the tables in your SQL 2005 db for you. Please post your comments, changes, etc. and I'll update this throughout: select distinct t.table_schema + '.' + t.table_name as [table] , case when tc.constraint_type = 'FOREIGN KEY' then 1 else 2 end as [order] into #master_tbl from information_schema.tables t inner join information_schema.table_constraints tc on tc.table_schema = t.table_schema and tc.table_name = t.table_name where t.table_type = 'BASE TABLE' and tc.constraint_type like '% KEY' order by 2
alter table #master_tbl add idx bigint identity(1,1)
select * into #k from #master_tbl
select * into #rk from #k
while ((select count(*) from #k) > 0) begin
declare @table varchar(1024) declare @idx bigint
select top 1 @table = [table] , @idx = idx from #k
exec ('alter table ' + @table + ' nocheck constraint all')
delete from #k where idx = @idx
end
drop table #k
select * into #t from #master_tbl
while ((select count(*) from #t) > 0) begin
declare @tidx bigint declare @ttable varchar(1024)
select top 1 @tidx = idx , @ttable = [table] from #t order by [order]
begin try exec('truncate table ' + @ttable) print @ttable + ' purged' end try begin catch print @ttable + ' could not be purged.' end catch
delete from #t where idx = @tidx
end
drop table #t
while ((select count(*) from #rk) > 0) begin
declare @ktable varchar(1024) declare @kidx bigint
select top 1 @ktable = [table] , @kidx = idx from #rk
exec ('alter table ' + @ktable + ' with check check constraint all')
delete from #rk where idx = @kidx
end
drop table #rk
drop table #master_tbl
Cheers...
Archives
- September, 2011 (1)
- June, 2011 (2)
- February, 2011 (1)
- January, 2011 (2)
- December, 2010 (1)
- October, 2010 (1)
- September, 2010 (2)
- August, 2010 (3)
- July, 2010 (5)
- May, 2010 (1)
- April, 2010 (1)
- December, 2009 (1)
- November, 2009 (1)
- October, 2009 (2)
- September, 2009 (2)
- August, 2009 (2)
- July, 2009 (1)
- June, 2009 (1)
- May, 2009 (3)
- April, 2009 (4)
- March, 2009 (3)
- February, 2009 (3)
- January, 2009 (3)
- December, 2008 (1)
- November, 2008 (2)
- October, 2008 (4)
- September, 2008 (1)
- August, 2008 (1)
- July, 2008 (3)
- June, 2008 (3)
- May, 2008 (4)
- April, 2008 (1)
- March, 2008 (2)
- February, 2008 (1)
- January, 2008 (3)
- December, 2007 (3)
- September, 2007 (1)
- August, 2007 (1)
- June, 2007 (1)
- May, 2007 (2)
- February, 2007 (1)
- December, 2006 (2)
- November, 2006 (4)
- September, 2006 (1)
- August, 2006 (1)
- May, 2006 (3)
Post Categories
| |
| 30 | 31 | 1 | 2 | 3 | 4 | 5 | | 6 | 7 | 8 | 9 | 10 | 11 | 12 | | 13 | 14 | 15 | 16 | 17 | 18 | 19 | | 20 | 21 | 22 | 23 | 24 | 25 | 26 | | 27 | 28 | 29 | 30 | 31 | 1 | 2 | | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|