Nabídka portálu se při zobrazení článku přesouvá na konec pravého sloupce nabídek.
Jak smazat obsah databáze |
Autor: Filip Yaghob
| Zdroj: Vlastní tvorba, publikovaná již na SQLServerCentral.com (http://www.sqlservercentral.com/scripts/contributions/746.asp)
| Vytvořeno: 25.9.2003
| Publikováno: 28.9.2003
| Čtenářů: 7037
| Unikátních: 6875
|
Někdy je třeba smazat obsah (strukturu) databáze bez toho, že by se smazala celá databáze a znova vytvářela (zachovají se uživatelé a nastavení). Vyrobil jsem tedy uloženou proceduru, která vymaže komplet strukturu databáze. Často tuto proceduru používáme při kompletním rebuildu struktury databáze na začátku projektu.
Zde je ona procedura. Buďte s ní opatrní, není vhodné ji pouštět v produkční databázi :)
create procedure usp_cleardb
as
begin
declare @oname varchar(255)
declare @tname varchar(255)
declare @sql varchar(1024)
declare @counter int
declare c_f cursor
forward_only read_only
for
select a.name, b.name
from sysobjects a,
sysobjects b,
sysforeignkeys c
where (a.xtype = 'F') and
(c.constid = a.id) and
(b.id = c.fkeyid)
open c_f
select @counter = 0
fetch next from c_f into @oname, @tname
while @@fetch_status = 0
begin
select @sql = 'alter table [' + @tname +
'] drop constraint [' + @oname + ']'
exec (@sql)
select @counter = @counter + 1
fetch next from c_f into @oname, @tname
end
close c_f
deallocate c_f
print convert(varchar(5), @counter) +
' constraint(s) deleted'
declare c_p cursor
forward_only read_only
for
select a.name
from sysobjects a
where (a.xtype = 'P') and
(a.name not like 'dt_%') and
(a.name <> 'usp_cleardb')
open c_p
select @counter = 0
fetch next from c_p into @oname
while @@fetch_status = 0
begin
select @sql = 'drop procedure [' + @oname + ']'
exec (@sql)
select @counter = @counter + 1
fetch next from c_p into @oname
end
close c_p
deallocate c_p
print convert(varchar(5), @counter) +
' procedure(s) deleted'
declare c_fc cursor
forward_only read_only
for
select a.name
from sysobjects a
where (a.xtype in ('TF', 'IF', 'FN'))
open c_fc
select @counter = 0
fetch next from c_fc into @oname
while @@fetch_status = 0
begin
select @sql = 'drop function [' + @oname + ']'
exec (@sql)
select @counter = @counter + 1
fetch next from c_fc into @oname
end
close c_fc
deallocate c_fc
print convert(varchar(5), @counter) +
' function(s) deleted'
declare c_tr cursor
forward_only read_only
for
select a.name
from sysobjects a
where (a.xtype = 'TR') and
(a.name like 't__%')
open c_tr
select @counter = 0
fetch next from c_tr into @oname
while @@fetch_status = 0
begin
select @sql = 'drop trigger [' + @oname + ']'
exec (@sql)
select @counter = @counter + 1
fetch next from c_tr into @oname
end
close c_tr
deallocate c_tr
print convert(varchar(5), @counter) +
' trigger(s) deleted'
declare c_v cursor
forward_only read_only
for
select a.name, b.name
from sysobjects a, sysusers b
where (a.xtype = 'V') and
(a.name not like 'sys%') and
(b.uid = a.uid)
open c_v
select @counter = 0
fetch next from c_v into @tname, @oname
while @@fetch_status = 0
begin
select @sql = 'drop view ' + @oname +
'.[' + @tname + ']'
exec (@sql)
select @counter = @counter + 1
fetch next from c_v into @tname, @oname
end
close c_v
deallocate c_v
print convert(varchar(5), @counter) +
' view(s) deleted'
declare c_u cursor
forward_only read_only
for
select a.name, b.name
from sysobjects a, sysusers b
where (a.xtype = 'U') and
(a.name <> 'dtproperties') and
(b.uid = a.uid)
open c_u
select @counter = 0
fetch next from c_u into @tname, @oname
while @@fetch_status = 0
begin
select @sql = 'drop table ' + @oname +
'.[' + @tname + ']'
exec (@sql)
select @counter = @counter + 1
fetch next from c_u into @tname, @oname
end
close c_u
deallocate c_u
print convert(varchar(5), @counter) +
' table(s) deleted'
end
Někdy se může stát, že se nesmažou uživatelsky definované funkce, pokud jsou použité v definici computed sloupečku. Pokud to tak je, tak spusťte proceduru víckrát, dokud neproběhne bez chyb.
Uvítám všechny úpravy kódu, které tuto proceduru vylepší.
|