事务处理begin transtraction和比较经典的存储过程

来源:互联网 发布:wince导航软件 编辑:程序博客网 时间:2024/05/21 14:10
主  题:
关于begin trans 的用法!请大家帮忙!!!
小女子做了一个存储过程,是用来备份数据的.代码部分如下:  select * into #test  from  chat_record  insert into chat_record_history select *  from #test  delete chat_record where exists (select content from #test where chat_record.content=#test.content)将表chat_record中的数据先放到一临时表#test中.然后在将临时表#test中的数据放到历史表chat_record_history 中,最后删除chat_record表和临时表#test相同的记录.现在在insert into chat_record_history select *  from #test这个步骤出错后,也就是没有将记录插入到历史表中,但是它又删掉了,原来表的数据.现在想用begin trans.请问怎么用呀!!!我自己试了几次还是有问题.请大家帮忙
回复人: xogirl(xogirl) ( ) 信誉:97 2003-06-13 11:54:02Z 得分:0
?
up.强烈号召大力的到来.希望大家帮帮忙呀
Top
回复人: caiyunxia(monkey) ( ) 信誉:116 2003-06-13 12:00:37Z 得分:0
?
用触发期,效果更好CRETAE TRIGGER TR_NAME_DEL ON chat_recordFOR DELETEINSERT INTO record_historySELECT * FROM DELETED
Top
回复人: caiyunxia(monkey) ( ) 信誉:116 2003-06-13 12:03:11Z 得分:0
?
BEGIN TRAN  select * into #test  from  chat_record IF @@ERROR <> O GOTO QUITERROR  insert into chat_record_history select *  from #test IF @@ERROR <> O GOTO QUITERROR  delete chat_record where exists (select content from #test where chat_record.content=#test.content) IF @@ERROR <> O GOTO QUITERROR  COMMIT GOTO SAVEEND QUITERROR:  ROLLBACK SAVEEND:
Top
回复人: CrazyFor(冬眠的鼹鼠) ( ) 信誉:266 2003-06-13 12:04:02Z 得分:0
?
是不是数据已被删除了,试试这个工具恢复数据吧.Log Explorerhttp://download.heibai.net/download/show.php?id=3125解压缩密码 www.heibai.net注册机产生的是注册码,是两个用解压缩密码解开后,压缩包里也有一个注册机的
Top
回复人: caiyunxia(monkey) ( ) 信誉:116 2003-06-13 12:04:13Z 得分:0
?
还有没必要先插入临时表,直接插入历史表
Top
回复人: happydreamer() ( ) 信誉:204 2003-06-13 12:08:02Z 得分:0
?
DECLARE @select_error int, @insert_error int,@delete_error intBEGIN TRAN select * into #test  from  chat_record select @select_error = @@ERROR insert into chat_record_history select *  from #test select @insert_error=@@ERROR delete chat_record where exists (select content from #test where  chat_record.content=#test.content) select @delete_error=@@ERRORIF @select_error = 0 AND @insert_error = 0 AND @delete_error=0BEGIN      COMMIT TRANENDELSEBEGIN   IF @select_error <> 0       PRINT "select error"    IF @inser_error <> 0      PRINT "isnert error"    IF @delte_error <> 0      PRINT "delete error"    ROLLBACK TRANEND
Top
回复人: hjb111(波波哥,偷偷来上网!) ( ) 信誉:100 2003-06-13 12:12:16Z 得分:0
?
try:begin transtraction select * into #test  from  chat_record insert into chat_record_history select *  from #test if @@error<>0  rollback transaction  delete chat_record where exists (select content from #test where chat_record.content=#test.content)commit transtraction
Top
回复人: happydreamer() ( ) 信誉:204 2003-06-13 12:15:15Z 得分:0
?
执行insert into chat_record_history select *  from #test有什么出错提示么
原创粉丝点击