在sqlserver 中删除所有的非系统表的sql语句
DECLARE @name VARCHAR(128) DECLARE @SQL VARCHAR(254) SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'P' AND category = 0 ORDER BY [name]) WHILE @name is not null BEGIN SELECT @SQL = 'DROP PROCEDURE [dbo].[' + RTRIM(@name) + ']' EXEC (@SQL) PRINT 'Dropped Procedure: ' + @name SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'P' AND category = 0 AND [name] > @name ORDER BY [name]) END GO DECLARE @name VARCHAR(128) DECLARE @SQL VARCHAR(254) SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'V' AND category = 0 ORDER BY [name]) WHILE @name IS NOT NULL BEGIN SELECT @SQL = 'DROP VIEW [dbo].[' + RTRIM(@name) + ']' EXEC (@SQL) PRINT 'Dropped View: ' + @name SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'V' AND category = 0 AND [name] > @name ORDER BY [name]) END GO DECLARE @name VARCHAR(128) DECLARE @SQL VARCHAR(254) SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] IN (N'FN', N'IF', N'TF', N'FS', N'FT') AND category = 0 ORDER BY [name]) WHILE @name IS NOT NULL BEGIN SELECT @SQL = 'DROP FUNCTION [dbo].[' + RTRIM(@name) + ']' EXEC (@SQL) PRINT 'Dropped Function: ' + @name SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] IN (N'FN', N'IF', N'TF', N'FS', N'FT') AND category = 0 AND [name] > @name ORDER BY [name]) END GO DECLARE @name VARCHAR(128) DECLARE @constraint VARCHAR(254) DECLARE @SQL VARCHAR(254) SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' ORDER BY TABLE_NAME) WHILE @name is not null BEGIN SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME) WHILE @constraint IS NOT NULL BEGIN SELECT @SQL = 'ALTER TABLE [dbo].[' + RTRIM(@name) + '] DROP CONSTRAINT [' + RTRIM(@constraint) + ']' EXEC (@SQL) PRINT 'Dropped FK Constraint: ' + @constraint + ' on ' + @name SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' AND CONSTRAINT_NAME != @constraint AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME) END SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' ORDER BY TABLE_NAME) END GO DECLARE @name VARCHAR(128) DECLARE @constraint VARCHAR(254) DECLARE @SQL VARCHAR(254) SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' ORDER BY TABLE_NAME) WHILE @name IS NOT NULL BEGIN SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME) WHILE @constraint is not null BEGIN SELECT @SQL = 'ALTER TABLE [dbo].[' + RTRIM(@name) + '] DROP CONSTRAINT [' + RTRIM(@constraint)+ ']' EXEC (@SQL) PRINT 'Dropped PK Constraint: ' + @constraint + ' on ' + @name SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' AND CONSTRAINT_NAME != @constraint AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME) END SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' ORDER BY TABLE_NAME) END GO DECLARE @name VARCHAR(128) DECLARE @SQL VARCHAR(254) SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'U' AND category = 0 ORDER BY [name]) WHILE @name IS NOT NULL BEGIN IF(@name = 'SCHEMA_VERSION') SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'U' AND category = 0 AND [name] > @name ORDER BY [name]) ELSE BEGIN SELECT @SQL = 'DROP TABLE [dbo].[' + RTRIM(@name) + ']' EXEC (@SQL) PRINT 'Dropped Table: ' + @name SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'U' AND category = 0 AND [name] > @name ORDER BY [name]) END END GO
相关推荐
Sql Server 删除所有表数据语句 Sql Server 是一种关系型数据库管理系统,广泛应用于企业级应用程序中。在实际开发和测试中,我们常常需要删除数据库中的所有表数据,而保持表结构不变。下面我们将介绍如何使用一条...
sql语句删除清空一个数据中,所有表结构表数据,请谨慎操作
### SQL清空所有表 在数据库管理中,有时我们需要清空整个数据库中的所有表的数据,以便进行测试或初始化等操作。直接使用一个简单的命令来实现这一功能是非常实用且高效的。本文将详细介绍如何利用T-SQL脚本来批量...
### 删除表中重复数据SQL语句详解 #### 一、基础知识概述 在数据库管理中,重复数据是一个常见的问题,尤其当数据库经历长时间的数据输入时,可能会出现数据录入错误或者批量导入过程中导致的数据冗余。重复数据的...
恢复oracle中用pl_sql删除的表
在删除所有表后,架构变为“空”,此时可以删除架构本身。但要注意,如果架构中还有其他对象(如视图、存储过程等),则无法直接删除。删除架构的SQL语句如下: ```sql DROP SCHEMA [你的架构名]; -- 替换为实际...
在压缩包中的文件`表空间操作.sql`、`sql_创建表空间.sql`和`sql_删除表空间.sql`可能包含了具体的示例脚本,供你参考和实践这些操作。请根据实际需求进行修改和执行,以确保数据库的管理和组织符合你的业务需求。...
以下是一个简单的示例,该示例演示了如何删除表中早于六个月前的所有记录: ```sql -- 声明变量 @tm 用于存储六个月前的日期 DECLARE @tm DATETIME; -- 计算六个月前的日期 SELECT @tm = DATEADD(month, -6, ...
该语句可以删除一个或多个表中的所有行,但保留表的结构、属性和索引。DELETE 语句的基本语法如下: ```sql DELETE FROM table_name; ``` 其中,`table_name`是要删除行的表名。 例如,要删除名为 "Persons" 的表中...
### SQL删除表里重复数据的两种方法 在数据库管理中,数据的一致性和准确性是非常重要的。重复的数据不仅会占用额外的存储空间,还可能导致查询结果的不准确。因此,掌握如何有效地删除表中的重复记录是每个数据库...
部分代码如下 ...--删除表 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
批量删除具有相同前缀的表的sql语句,sqlserver
在本篇文章中,我们将详细探讨SQL删除的方法,包括删除单条记录、多条记录、整张表以及整个数据库的步骤与注意事项。 首先,我们来看如何删除单条或多条记录。SQL提供了`DELETE`语句来实现这一功能。如果要删除特定...
删除所有表可以通过遍历`sysobjects`表并获取所有类型为`U`的对象(表示用户定义的表),然后构建`DROP TABLE`语句来实现。具体步骤如下: ```sql -- 使用目标数据库 USE your_database_name; GO DECLARE @tname ...
删除数据库的所有表、存储过程、主键、外键、索引等 .sql
该SQL可以直接删除sqlserver数据库下所有触发器,直接在你想要删除触发器的数据库下执行即可。
SQL批量删除数据表中指定字段对应的所有约束
- 作业的步骤包括新建步骤,选择 SQL 脚本类型,输入 SQL 语句(如 `DELETE FROM 表名`),用于删除特定表的数据。 4. **设置作业调度**: - 作业的调度可以通过维护计划向导进行设置,如设置为每月、每周或每天...
### SQL删除注入数据的SQL脚本 #### 背景介绍 在数据库管理与维护过程中,数据安全至关重要。其中一种常见的安全威胁就是SQL注入攻击,它通过将恶意代码插入到SQL查询语句中来实现对数据库的非法访问或操作。为了...