Declare @t table(query varchar(2000),tables varchar(100))
Insert into @t
select 'Truncate table ['+T.table_name+']', T.Table_Name from INFORMATION_SCHEMA.TABLES T
left outer join INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
on T.table_name=TC.table_name
where (TC.constraint_Type ='Foreign Key' or TC.constraint_Type is NULL) and
T.table_name not in ('dtproperties','sysconstraints','syssegments') and
Table_type='BASE TABLE'
Insert into @t
select 'delete from ['+T.table_name+']', T.Table_Name from INFORMATION_SCHEMA.TABLES T
left outer join INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
on T.table_name=TC.table_name where TC.constraint_Type ='Primary Key' and T.table_name <>'dtproperties'and Table_type='BASE TABLE'
Declare @sql varchar(8000)
Select @sql=IsNull(@sql+' ','')+ query from @t
print(@sql)
Exec(@sql)
http://topic.youkuaiyun.com/u/20091215/09/33c8705f-b5d5-48e5-a18e-6894290e1620.html
本文介绍了一种使用T-SQL脚本批量生成删除记录和截断表命令的方法,适用于需要清空多个表数据的场景。通过声明临时表并结合INFORMATION_SCHEMA中的视图来获取数据库中所有基表的信息,进而生成相应的SQL命令。
276

被折叠的 条评论
为什么被折叠?



