SQL Server 删除数据库所有表和所有存储过程

场景:

SQL Server中,需要删除所有表或所有存储过程时,手动的方式只能逐个进行删除,耗个人时间,所以想弄个语句来实现这样的需求。

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

  1. 第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
  1. 第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)
 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 保留。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 SQL Server 提供的系统存储过程 sp_MSforeachtable 来遍历所有,并使用 SELECT 语句获取每个的数据。以下是一个 C++ 实现的示例代码: ```cpp #include <iostream> #include <sql.h> #include <sqlext.h> #define SQL_RESULT_LEN 240 #define SQL_RETURN_CODE_LEN 1000 using namespace std; int main() { // Declare variables SQLHANDLE sqlenvhandle; SQLHANDLE sqlconnectionhandle; SQLHANDLE sqlstatementhandle; SQLRETURN retcode; SQLCHAR retconstring[SQL_RETURN_CODE_LEN]; // Allocate environment handle retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &sqlenvhandle); // Set the ODBC version environment attribute if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { retcode = SQLSetEnvAttr(sqlenvhandle, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0); // Allocate connection handle if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { retcode = SQLAllocHandle(SQL_HANDLE_DBC, sqlenvhandle, &sqlconnectionhandle); // Set the connection timeout attribute if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { SQLSetConnectAttr(sqlconnectionhandle, SQL_LOGIN_TIMEOUT, (SQLPOINTER)5, 0); // Connect to SQL Server retcode = SQLConnect(sqlconnectionhandle, (SQLCHAR*)"your_server_name", SQL_NTS, (SQLCHAR*)"your_username", SQL_NTS, (SQLCHAR*)"your_password", SQL_NTS); // Allocate statement handle if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { retcode = SQLAllocHandle(SQL_HANDLE_STMT, sqlconnectionhandle, &sqlstatementhandle); // Loop through all tables and fetch data if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { retcode = SQLExecDirect(sqlstatementhandle, (SQLCHAR*)"EXEC sp_MSforeachtable 'SELECT * FROM ?'", SQL_NTS); // Fetch and print data for each table while (retcode == SQL_SUCCESS) { SQLCHAR table_name[SQL_RESULT_LEN]; SQLINTEGER len; retcode = SQLFetch(sqlstatementhandle); if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { SQLGetData(sqlstatementhandle, 1, SQL_C_CHAR, table_name, SQL_RESULT_LEN, &len); cout << "Data for table " << table_name << ":" << endl; SQLSMALLINT num_cols; SQLNumResultCols(sqlstatementhandle, &num_cols); while (SQLFetch(sqlstatementhandle) != SQL_NO_DATA) { for (int i = 1; i <= num_cols; i++) { SQLCHAR col_name[SQL_RESULT_LEN]; SQLCHAR col_value[SQL_RESULT_LEN]; SQLINTEGER col_len; SQLDescribeCol(sqlstatementhandle, i, col_name, SQL_RESULT_LEN, &len, NULL, NULL, NULL, NULL); SQLGetData(sqlstatementhandle, i, SQL_C_CHAR, col_value, SQL_RESULT_LEN, &col_len); cout << col_name << ": " << col_value << ", "; } cout << endl; } } } } } } } } // Free resources SQLFreeHandle(SQL_HANDLE_STMT, sqlstatementhandle); SQLDisconnect(sqlconnectionhandle); SQLFreeHandle(SQL_HANDLE_DBC, sqlconnectionhandle); SQLFreeHandle(SQL_HANDLE_ENV, sqlenvhandle); return 0; } ``` 请注意替换代码中的 "your_server_name"、"your_username" 和 "your_password" 为你的 SQL Server 服务器名称、用户名和密码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值