要刪除某個資料庫,或者刪除資料庫中的所有表(刪除表結構和資料),需要先刪除表間的外來鍵約束,才能刪除表。如刪除資料庫db_wy中的所有表:
--/第1步**********刪除所有表的外來鍵約束*************************/declare c1 cursor for
select 'alter table ['+ object_name(parent_obj) + '] drop constraint ['+name+']; '
from sysobjects
where xtype = 'f'
open c1
declare @c1 varchar(8000)
fetch next from c1 into @c1
while(@@fetch_status=0)
begin
exec(@c1)
fetch next from c1 into @c1
endclose c1
deallocate c1
--/第2步**********刪除所有表*************************/
use db_wy
godeclare @sql varchar(8000)
while (select count(*) from sysobjects where type='u')>0
begin
select @sql='drop table ' + name
from sysobjects
where (type = 'u')
order by 'drop table ' + name
exec(@sql)
end
Sql Server 刪除所有表
如果由於外來鍵約束刪除table失敗,則先刪除所有約束 第1步 刪除所有表的外來鍵約束 declarec1cursorfor select alter table object name parent obj drop constraint name fromsysobjects wherextyp...
sqlserver刪除所有的使用者表
create procedure dbo.deletetable as declare name as nvarchar 128 declare curobject cursor for select name from sysobjects where type u and name like p...
sqlserver 2008 刪除所有表
declare c cursor for 定義游標 select name from sysobjects where xtype u 查詢所有表 declare t varchar 20 open c fetch next from c into t while fetch status 0 be...