關於存儲過程的執行錯誤,怎麼把撤消已執行的語句影響?? 謝謝!!

tmj 2003-11-06 09:25:01
關於存儲過程的執行錯誤, 存儲過程執行一些語句后因語句錯誤而不繼續執行,怎麼把撤消已執行的語句的結果
...全文
31 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
tmj 2003-11-06
  • 打赏
  • 举报
回复
to pengdali(大力 V3.0) killxtt(我菜我菜我菜菜菜)

CREATE PROCEDURE testa AS
SET XACT_ABORT on

begin tran

delete from testc

select * from testb

commit tran
GO

Testc中有記錄 把testb改名 testb不存在,此過程執行出錯,但testc中的刪除的記錄不會恢復

錯誤為:

Server: Msg 208, Level 16, State 1, Procedure testa, Line 8
Invalid object name 'testb'.
Server: Msg 266, Level 16, State 1, Procedure testa, Line 10
Transaction count after EXECUTE indicates that a COMMIT or ROLLBACK TRANSACTION statement is missing. Previous count = 1, current count = 2.


killxtt 2003-11-06
  • 打赏
  • 举报
回复
是用transaction阿
如果有错就rollback,回滚了不就撤销了吗
如果正确就commit,提交了阿
tmj 2003-11-06
  • 打赏
  • 举报
回复
to txlicenhe(马可) 謝謝!

是在有執行錯誤時撤消,如果沒有執行錯誤就不要撤消

但不知道那條出錯
pengdali 2003-11-06
  • 打赏
  • 举报
回复
SET XACT_ABORT on ----设置
go

create table #Table1 (a tinyint)
go
begin tran
insert #table1 values(1) ----成功
insert #table1 values(1000) ----这句将报错
commit tran
go
select * from #table1
go
drop table #table1
pengdali 2003-11-06
  • 打赏
  • 举报
回复
create proc xx
as
...

begin tran
insert ..
if @@error<>0 goto error
update ...
if @@error<>0 goto error
commit tran
return

error:
rollback tran
txlicenhe 2003-11-06
  • 打赏
  • 举报
回复
begin tran
insert
update
...
rollback -- 撤消从 begin tran开始的语句。
pengdali 2003-11-06
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/TopicView1.asp?id=1675986

34,875

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧