村长

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

出自:http://blog.csdn.net/zhwqy84/article/details/6869549

 

DECLARE c1 cursor for 
    select 'alter table ['+ object_name(parent_obj) + '] dropconstraint ['+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
go


declare @tname varchar(8000)
 set @tname=''
 select @tname=@tname + Name + ',' from sysobjects where xtype='U'
 select @tname='drop table ' + left(@tname,len(@tname)-1)
 exec(@tname)

go

declare @tname varchar(8000)
 set @tname=''
 select @tname=@tname + Name + ',' from sysobjects where xtype='P'
 select @tname='drop Procedure ' + left(@tname,len(@tname)-1)
 exec(@tname)

go

declare @tname varchar(8000)
 set @tname=''
 select @tname=@tname + Name + ',' from sysobjects where xtype='V'
 select @tname='drop View ' + left(@tname,len(@tname)-1)
 exec(@tname)

go

  

一次性删除数据库所有表和所有存储过程 SQL语句

删除所有的表:

如果由于外键约束删除table失败,则先删除所有约束:

--/第1步**********删除所有表的外键约束*************************/

DECLARE c1 cursor for     select 'alter table ['+ object_name(parent_obj) + '] dropconstraint ['+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步**********删除所有表*************************/

use 数据库 declare @tname varchar(8000) set @tname='' select @tname=@tname + Name + ',' from sysobjects where xtype='U' select @tname='drop table ' + left(@tname,len(@tname)-1) exec(@tname)

删除所有的存储过程同理可得,但不需要走第一步,只需将第2步的代码的wherextype='U' 改成 wherextype='P',drop table 改成 dropProcedure

删除所有的视图同理可得,但不需要走第一步,只需将第2步的代码的wherextype='U' 改成 where xtype='V',drop table 改成 drop View

 

sysobjects的xtype代表含义:

在数据库内创建的每个对象(约束、默认值、日志、规则、存储过程等)在表中占一行。只有在 tempdb内,每个临时对象才在该表中占一行。
列名 数据类型 描述 name sysname 对象名。 Id int 对象标识号。 xtype char(2) 对象类型。可以是下列对象类型中的一种: C = CHECK 约束 D = 默认值或 DEFAULT 约束 F = FOREIGN KEY 约束 L = 日志 FN = 标量函数 IF = 内嵌表函数 P = 存储过程 PK = PRIMARY KEY 约束(类型是 K) RF = 复制筛选存储过程 S = 系统表 TF = 表函数 TR = 触发器 U = 用户表 UQ = UNIQUE 约束(类型是 K) V = 视图 X = 扩展存储过程
uid smallint 所有者对象的用户 ID。 info smallint 保留。仅限内部使用。 status int 保留。仅限内部使用。 base_schema_ ver int 保留。仅限内部使用。 replinfo int 保留。供复制使用。 parent_obj int 父对象的对象标识号(例如,对于触发器或约束,该标识号为表 ID)。 crdatedatetime 对象的创建日期。 ftcatid smallint 为全文索引注册的所有用户表的全文目录标识符,对于没有注册的所有用户表则为 0。 schema_ver int 版本号,该版本号在每次表的架构更改时都增加。 stats_schema_ ver int 保留。仅限内部使用。 type char(2) 对象类型。可以是下列值之一: C = CHECK 约束 D = 默认值或 DEFAULT 约束 F = FOREIGN KEY 约束 FN = 标量函数 IF = 内嵌表函数 K = PRIMARY KEY 或 UNIQUE 约束 L = 日志 P = 存储过程 R = 规则 RF = 复制筛选存储过程 S = 系统表 TF = 表函数 TR = 触发器 U = 用户表 V = 视图 X = 扩展存储过程
userstat smallint 保留。 sysstat smallint 内部状态信息。 indexdel smallint 保留。 refdate datetime 留作以后使用。 version int 留作以后使用。 deltrig int 保留。 instrig int 保留。 updtrig int 保留。 seltrig int 保留。 category int 用于发布、约束和标识。 cache smallint 保留。   

posted on 2011-12-07 20:51  Say No  阅读(825)  评论(0编辑  收藏  举报