Sybase 中的错误处理 [英] Error Handling in Sybase

查看:37
本文介绍了Sybase 中的错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法处理SYBASE中的错误,比如TRY-CATCH块,你可以在MS SQL Server中使用,<代码>Oracle 等?

Is there a way to handle errors in SYBASE, such as the TRY-CATCH block you can use in MS SQL Server, Oracle, etc?

我在网上搜索过,我找到的唯一选项是全局变量 @@error,但它没有按我预期的那样工作,例如,以下代码:

I've searched the web and the only option I found was the global variable @@error, but it didn' work as I expected, for example, the following code:

begin tran

update table1
set name = 'new name'
where name = 'old name'

update table2
set id = 1 
where id = 30
-- suppose id has a unique constraint and there's already a row with id = 1

IF @@error = 0
begin
    print 'commited'
    commit
end
else
begin
    print 'rolled back'
    rollback
end

确实会以某种方式回滚,因为我在 table1 上更改的名称保留了我在此处测试的旧值,但它不会打印消息,也不会执行我在导致错误

The will indeed rollback somehow, because the name I've changed on table1 keeps the old value as I've tested here, but it doesn't print the messages, or execute any instructions I put after the instructions that causes the error

有人可以帮我吗?您知道 Sybase 错误处理实际上是如何工作的吗?

Can anyone help me in this? Do you know how does Sybase error handling actually works?

推荐答案

第一个解决方案.

您无法在 Sybase 上以这种方式捕获异常.在更新之前,您必须检查数据:

You can't catch an exception this way on Sybase. Before you update you have to check data:

if not exists
(
  select 1 from table2
  where id = 1
)
begin
  update table2
  set id = 1 
  where id = 30
end
else
begin
  print 'rolled back'
  rollback
end

第二种解决方案.

你也可以给procedure 写一个update 命令,然后你就可以捕获异常了.创建过程:

You can also put an update command to procedure, then you can catch an exception. Create procedure:

create procedure myproc
as
begin
  update table2
  set id = 1 
  where id = 30
end

并按如下方式运行:

begin tran

update table1
set name = 'new name'
where name = 'old name'

exec myproc

IF @@error = 0
begin
    print 'commited'
    commit
end
else
begin
    print 'rolled back'
    rollback
end

这篇关于Sybase 中的错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆