清空数据库中表记录

/*--------------------------------------------
--清空数据库中表记录,允许排除某些表
--yangys  2006/03/29
*/

--首先独立删除外健约束
declare @strsql varchar(8000)
declare curTemp cursor local for
select s='alter table ['+object_name(parent_obj)+'] drop constraint ['+name+']'
from sysobjects where xtype ='F'

open curTemp
fetch next from curTemp into @strsql
while @@fetch_status=0
begin
 exec(@strsql)
 fetch next from curTemp into @strsql
end
close curTemp
deallocate curTemp
go

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tmpSource]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[tmpSource]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tmpSpec]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[tmpSpec]
GO

--创建临时表
--所有表名称信息
create table tmpSource
(
 tableName varchar(100)
)

--要删除表信息
create table tmpSpec
(
 tableName varchar(100)
)
go

--插入要删除表信息
insert tmpSpec
select 'InfoFormType'
union all
select 'FormTypeID'
union all
select 'InfoSettingModule'
union all
select 'InfoSettingModule'
union all
select 'permit_info'
union all
select 'ManufacturePlanType'
union all
select 'PrintLableName'
union all
select 'ManufacturePlanGrade'
union all
select 'QualityMethodType'
union all
select 'StkInType'
union all
select 'StkOutType'
union all
select 'Stk'
union all
select 'InfoOperator'
union all
select 'InfoCurrency'


go

--插入表名信息
insert tmpSource
select [name] from dbo.sysobjects where xtype='U' and [name]<>'dtproperties' And [Name] <>'MSreplication_options'

--删除不需要清空的表
delete from tmpSource
where exists(select 1 from tmpSpec where tableName=tmpSource.tableName)

go

--执行删除
declare @str varchar(8000)
set @str=''

select @str=@str+[tableName] +'   , ' from tmpSource

declare @strCommand varchar(2000)
set @strCommand='delete  '
set @str=left(@str,len(@str)-1)
set @str= replace(@str,',',@strCommand)

set @str=@strCommand+' '+@str

exec(@str)

go

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值