您的位置:首页 > 数据库

【SQLSERVER】清空数据库所有表数据

2013-11-15 10:05 330 查看
[sql] view plaincopyprint?
-------清空数据库所有表数据
exec sp_MSforeachtable "truncate table ?"

----查询数据库所有表名
select [name] from sysobjects where type='u'

--- 游标 清空所有表数据
declare @tableName varchar(50)
declare @Sql nvarchar(200)
declare @count int
declare TBCursor cursor for select [name] from sysobjects where type='u'
open TBCursor
fetch next from  TBCursor into @tableName
while @@fetch_status=0
begin
set @Sql=N'delete from '+  @tableName
exec sp_executesql @Sql --过程 sp_executesql,第 1 行 过程需要类型为 'ntext/nchar/nvarchar' 的参数 '@statement'。
fetch next from TBCursor into @tableName
end
close TBCursor
deallocate TBCursor

-----向 IntKey 表 插入数据库表名
insert into IntKey(KeyName)
select [name] from sysobjects where type='u'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: