当前位置:网站首页>SQL Server 删除数据库所有表和所有存储过程
SQL Server 删除数据库所有表和所有存储过程
2022-06-13 01:59:00 【司马弈】
场景:
SQL Server中,需要删除所有表或所有存储过程时,手动的方式只能逐个进行删除,耗个人时间,所以想弄个语句来实现这样的需求。
如果由于外键约束删除table失败,则先删除所有约束:
--/第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
go
--/第2步**********删除所有表*************************/
use 数据库
declare @tname varchar(8000)
set @tname=''
select @[email protected] + Name + ',' from sysobjects where xtype='U'
select @tname='drop table ' + left(@tname,len(@tname)-1)
exec(@tname)
go
删除所有的存储过程同理可得,但不需要走第一步,只需将第2步的代码的where xtype='U' 改成 where xtype='P',drop table 改成 drop Procedure
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)。
crdate datetime 对象的创建日期。
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 保留。
边栏推荐
- Devaxpress Chinese description -- tdxgallerycontrol object (gallery component)
- QT realizes mind mapping function (II)
- 开发者来稿|AMD赛灵思中文论坛分享 - 提问的智慧
- 分享三个关于CMDB的小故事
- Delphi7 compressed pictures (BMP, JPG, PNG)
- Vs how to enter chromium subprocess debugging
- What did Hello travel do right for 500million users in five years?
- Ctrip reshapes new Ctrip
- LabVIEW large project development tools to improve quality
- Use mediapipe+opencv to make a simple virtual keyboard
猜你喜欢

Magics 23.0 how to activate and use the slice preview function of the view tool page
![[the second day of the actual combat of the smart lock project based on stm32f401ret6 in 10 days] light up with the key ----- input and output of GPIO](/img/98/77191c51c1bab28448fe197ea13a33.jpg)
[the second day of the actual combat of the smart lock project based on stm32f401ret6 in 10 days] light up with the key ----- input and output of GPIO

Use mediapipe+opencv to make a simple virtual keyboard

Detailed explanation of maxpooling corresponding to conv1d, conv2d and conv3d machines of tensorflow2

Why is Huawei matebook x Pro 2022 leading a "laptop" revolution

3、 Upload fabric photos to SQL server and provide name to display fabric photos
![[the third day of actual combat of smart lock project based on stm32f401ret6 in 10 days] communication foundation and understanding serial port](/img/82/ed215078da0325b3adf95dcd6ffe30.jpg)
[the third day of actual combat of smart lock project based on stm32f401ret6 in 10 days] communication foundation and understanding serial port

华为设备配置IP和虚拟专用网混合FRR

Looking at Qianxin's "wild prospect" of network security from the 2021 annual performance report

四、入库管理功能的完善
随机推荐
华为设备配置双反射器优化虚拟专用网骨干层
Why is "iFLYTEK Super Brain 2030 plan" more worthy of expectation than "pure" virtual human
LabVIEW large project development tools to improve quality
水管工游戏
Quickly set the computer to turn off automatically
Looking at Qianxin's "wild prospect" of network security from the 2021 annual performance report
[the 4th day of the 10 day smart lock project based on stm32f401ret6] what is interrupt, interrupt service function, system tick timer
Examples of using the chromium base library
Delphi7 compressed pictures (BMP, JPG, PNG)
C语言压缩字符串保存到二进制文件,从二进制文件读取压缩字符串后解压。
万字讲清 synchronized 和 ReentrantLock 实现并发中的锁
Calculation of accuracy, recall rate, F1 value and accuracy rate of pytorch prediction results (simple implementation)
如何解决通过new Date()获取时间写出数据库与当前时间相差8小时问题【亲测有效】
Developer contributions amd Xilinx Chinese Forum sharing - wisdom of questioning
Parameter measurement method of brushless motor
一、搭建django自动化平台(实现一键执行sql)
The commercial value of Kwai is being seen by more and more brands and businesses
Répertoire d'exclusion du transport rsync
分享三个关于CMDB的小故事
华为设备配置CE双归属