号外:
一直在做公司的crm系统维护,今天早上做改动的时候,不小心一个存储过程就把数据库整库truncate了。还好数据一直都有备份,而且SQL server的图形界面操作强大,很简单就能还原,唯一的意外就是我从来没试过还原系统。
就是以下这段代码祸害的,方法是建立一个临时表mytemp,临时表中两列,分别是sql语句和行号。SQL语句的内容都是truncate table +表名,总共4百多个表,执行exec命令全都清空了。
killer自白:
declare @str varchar(200),@countrow int
select 'truncate table '+name+'' as NAME,ROW_NUMBER() over(order by name ) as ID into #mytemp from sysobjects where type = 'U'
select @countrow = COUNT(1) from sysobjects where type = 'U'
while @countrow>0
begin
select @str = NAME from #mytemp where ID = @countrow
exec(@str)
select @countrow = @countrow - 1
end