新聞中心
使用SQL語(yǔ)句,如何才能批量刪除用戶(hù)表呢?下面就將為您介紹SQL批量刪除用戶(hù)表(先刪除所有外鍵約束,再刪除所有表) 的方法,供您參考,希望對(duì)您有所幫助。

--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
end
close c1
deallocate c1
--2.刪除表
DECLARE c2 cursor for
select 'drop table ['+name +']; '
from sysobjects
where xtype = 'u'
open c2
declare @c2 varchar(8000)
fetch next from c2 into @c2
while(@@fetch_status=0)
begin
exec(@c2)
fetch next from c2 into @c2#p#
end
close c2
deallocate c2
--批量清除表內(nèi)容:
--1.禁用外鍵約束
DECLARE c1 cursor for
select 'alter table ['+ object_name(parent_obj) + '] nocheck 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
end
close c1
deallocate c1
--2.清除表內(nèi)容
DECLARE c2 cursor for
select 'truncate table ['+name +']; '
from sysobjects
where xtype = 'u' #p#
open c2
declare @c2 varchar(8000)
fetch next from c2 into @c2
while(@@fetch_status=0)
begin
exec(@c2)
fetch next from c2 into @c2
end
close c2
deallocate c2
--3.啟用外鍵約束
DECLARE c1 cursor for
select 'alter table ['+ object_name(parent_obj) + '] check 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
end
close c1
deallocate c1
標(biāo)題名稱(chēng):SQL批量刪除用戶(hù)表的方法
本文地址:http://m.fisionsoft.com.cn/article/dpiodje.html


咨詢(xún)
建站咨詢(xún)
