博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
清空数据库错误:因为该表正由 FOREIGN KEY 约束引用 解决办法
阅读量:4542 次
发布时间:2019-06-08

本文共 1291 字,大约阅读时间需要 4 分钟。

如下解决了五个问题 

1. 清空数据 
2. 有外键也可以, 因为是逆向删除, 从最后一张表删除. 且使用的是delete, 因为truncate不能对有外键的表 
3. 种子问题, 如果表存在种子重设为0, 如不存在就不操作 
4. 加了事务, 中间报错, 有后悔机会 
5. 截断日志功能, 因为使用delete, 删除后日志文件会增大, 可以不使用

if( object_id('pr_DataClear') is not null )

drop procedure pr_DataClear
go
create procedure pr_DataClear
as
begin transaction
declare @cTblName varchar(128)
declare cur_Clear cursor for select rtrim(name) from sysobjects where type = 'U' order by crdate desc
open cur_Clear
declare @cSQL varchar(255)
fetch next from cur_Clear into @cTblName
while( @@fetch_status = 0)
begin
set @cSQL = 'delete from ' + @cTblName
print @cSQL
exec( @cSQL )
if( ident_seed(@cTblName) is not null )
begin
dbcc checkident( @cTblName, reseed, 0 )
print '有种子且成功重置为1'
end
fetch next from cur_Clear into @cTblName
end
close cur_Clear
deallocate cur_Clear
commit
go
-- 清空所有表数据
exec pr_DataClear
-- 截断日志
backup log LZ的数据库 with no_log
dbcc shrinkdatabase( LZ的数据库 )
dbcc updateusage( LZ的数据库 )
-- 查看表空间(概数)
select object_name(id) as 表名, (rtrim(8*reserved/1024) + 'MB') as 总量, (rtrim(8*dpages/1024) + 'MB') as 已使用,
(rtrim(8*(reserved-dpages)/1024) + 'MB') as 未使用, (rtrim(8*dpages/1024-rows/1024*minlen/1024) + 'MB' ) as 空隙
from sysindexes
where indid=1
order by reserved desc

转载于:https://www.cnblogs.com/systemnet123/archive/2013/04/26/3045417.html

你可能感兴趣的文章
Python 元组
查看>>
Android——四大组件、六大布局、五大存储
查看>>
Socket实现原理和机制
查看>>
luogu1265 公路修建
查看>>
WORD2003电子签名插件(支持手写、签章)
查看>>
Google开源项目二维码读取与生成工具ZXing
查看>>
Android使用默认样式创建View的几个姿势
查看>>
多标记学习--Learning from Multi-Label Data
查看>>
leetcode 47. 全排列 II
查看>>
1083 List Grades (25)
查看>>
使用iptables和tc对端口限速
查看>>
14、反射(reflect)
查看>>
树-哈夫曼树
查看>>
异步复位同步释放
查看>>
【Spark2.0源码学习】-7.Driver与DriverRunner
查看>>
poj 1206
查看>>
Spring基础系列-参数校验
查看>>
php+ajax实现图片文件上传功能
查看>>
SQL日期格式化及创建相关日期
查看>>
模式识别--概率密度
查看>>