删除数据中的所有对象

参照于邹建出版的书

/*----------------------------------------------------------------------
--删除数据中的所有对象
--默认值或DEFAULT约束;FOREIGN KEY约束;CHECK约束;PRIMARY KEY 约束;UNIQUE约束
--触发器;表函数;视图;存储过程
--yangys 2006/03/29
--请严重主意:删除默认值对插入表记录使用时的影响
-----------------------------------------------------------------------*/
/*-----------------------------------------------------------
--删除数据库中的所有:
--DEFAULT约束;FOREIGN KEY约束;CHECK约束;PRIMARY KEY 约束;UNIQUE约束
*/
--首先独立删除外健约束
declare @strsql varchar(8000)
declare curTemp cursor local for
select s='alter table ['+object_name(parent_obj)+'] drop constraint ['+name+']'
from sysobjects where xtype ='F'

open curTemp
fetch next from curTemp into @strsql
while @@fetch_status=0
begin
 exec(@strsql)
 fetch next from curTemp into @strsql
end
close curTemp
deallocate curTemp
go

--这里会删除默认值
--DEFAULT约束CHECK约束;PRIMARY KEY 约束;UNIQUE约束
declare @strsql varchar(8000)
declare curTemp cursor local for
select s='alter table ['+object_name(parent_obj)+'] drop constraint ['+name+']'
from sysobjects where xtype in('F','C','D','PK','UQ')

open curTemp
fetch next from curTemp into @strsql
while @@fetch_status=0
begin
 exec(@strsql)
 fetch next from curTemp into @strsql
end
close curTemp
deallocate curTemp

go

/*-------------------------------------------------
--删除数据库中的所有:
--触发器;表函数;视图;存储过程
*/
declare @strsql varchar(8000)
declare curTemp cursor local for
select
 s='drop ' +
 case xtype
  when N'TR' then 'trigger'
  when N'FN' then 'function'
  when N'V' then 'view'
  when N'P' then 'proc'
  else '' end
       +' ['+name+']'
from sysobjects where xtype in('TR','FN','V','P') and [name]<>'syssegments' and [name]<>'sysconstraints'


open curTemp
fetch next from curTemp into @strsql
while @@fetch_status=0
begin
 exec(@strsql)
 fetch next from curTemp into @strsql
end
close curTemp
deallocate curTemp

go

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值