删除数据库所有表的数据

q520525745 2010-01-14 03:58:27
我想删除数据库A里的每一张表里面的数据
请知道该命令的说下3Q
...全文
997 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
lvxiaoming_99 2010-09-05
  • 打赏
  • 举报
回复
这个问题我找了很多,但是一直没有找到
phl2009 2010-01-14
  • 打赏
  • 举报
回复
学习
obuntu 2010-01-14
  • 打赏
  • 举报
回复
学习。
答案就在以上几楼中。
h821291419 2010-01-14
  • 打赏
  • 举报
回复

--先禁用所有外键约束
exec sp_msforeachtable "alter table ? nocheck CONSTRAINT all"
--然后删除数据
exec sp_msforeachtable @command1="truncate table ? ;",
@whereand='and schema_id = (select schema_id from sys.schemas )'
--再启用所有外键约束
exec sp_msforeachtable "alter table ? check constraint all"

yananguo_1985 2010-01-14
  • 打赏
  • 举报
回复
先要删除掉表上的约束
q520525745 2010-01-14
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 yananguo_1985 的回复:]
SQL codeselect'truncate table'+ namefrom sys.tableswhere type='U'

复制这个结果,然后执行即可!
[/Quote]还是不可以
前面几张表里就有数据了。
yananguo_1985 2010-01-14
  • 打赏
  • 举报
回复

select 'truncate table '+ name from sys.tables where type='U'

复制这个结果,然后执行即可!
喜-喜 2010-01-14
  • 打赏
  • 举报
回复
学习
--小F-- 2010-01-14
  • 打赏
  • 举报
回复
exec sp_msforeachtable @command1="truncate table ? ;",
@whereand='and schema_id = (select schema_id from sys.schemas )'




Mr_Nice 2010-01-14
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 fredrickhu 的回复:]
SQL code--删除外键约束DECLARE c1cursorforselect'alter table ['+object_name(parent_obj)+'] drop constraint ['+name+'];'from sysobjectswhere xtype='F'open c1declare@c1varchar(8000)fetchnextfrom c1into@c1while?-
[/Quote]

细致,要先消去外键约束才可以顺利的删除表中数据的。
xiequan2 2010-01-14
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 q520525745 的回复:]
我的意思是数据库A里有几百张表,现在要把这几百张表里的数据全部清空
有没有一条语句就可以直接实现而不需要每张表的表名打进命令里
貌似上面的都试过不可以。
[/Quote]

把数据库结构导出为sql角本,然后删除这个数据库,再执行这个角本
q520525745 2010-01-14
  • 打赏
  • 举报
回复
我的意思是数据库A里有几百张表,现在要把这几百张表里的数据全部清空
有没有一条语句就可以直接实现而不需要每张表的表名打进命令里
貌似上面的都试过不可以。
q93116854 2010-01-14
  • 打赏
  • 举报
回复
纯学习
水族杰纶 2010-01-14
  • 打赏
  • 举报
回复
UP
--小F-- 2010-01-14
  • 打赏
  • 举报
回复
--删除外键约束
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
--删除表
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(@@fetch_status=0)
begin
exec(@c2)
fetch next from c2 into @c2
end
close c2
deallocate c2



exec sp_msforeachtable @command1="truncate table ? ;",
@whereand='and schema_id = (select schema_id from sys.schemas where [name] not in(''khda'',''khzh'',''khzmx''))'


andysun88 2010-01-14
  • 打赏
  • 举报
回复

-- 打开隐式事务
SET IMPLICIT_TRANSACTIONS ON
-- 执行动态删除
begin tran
declare @sql varchar(max);
set @sql='';
select @sql=@sql+'delete from '+
QUOTENAME(SCHEMA_NAME([schema_id]))+'.'+QUOTENAME([name])+';'
from A.sys.tables where is_ms_shipped =0
--select @sql;
EXEC(@sql);
-- 正确,则提交事务,确认删除
commit tran
-- 不正确,则回滚事务,取消删除
rollback tran
-- 关闭隐式事务
SET IMPLICIT_TRANSACTIONS OFF
qiqi860819 2010-01-14
  • 打赏
  • 举报
回复

exec sp_MSforeachtable @command1=‘truncate from ?’
SQL77 2010-01-14
  • 打赏
  • 举报
回复
  --删除当前数据库所有表中的数据
sp_MSforeachtable @command1='Delete from ?'
sp_MSforeachtable @command1 = "TRUNCATE TABLE ?"
bancxc 2010-01-14
  • 打赏
  • 举报
回复
SP_MSFOREACHTABLE
SQL77 2010-01-14
  • 打赏
  • 举报
回复
可以用游标,也可以用SP_MSFOREACHTABLE
加载更多回复(1)

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧