Error Handling in Sybase

前端 未结 1 1047
执笔经年
执笔经年 2020-12-19 15:52

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

相关标签:
1条回答
  • 2020-12-19 16:17

    1st solution.

    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
    

    2nd solution.

    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
    

    and run it as below:

    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
    
    0 讨论(0)
提交回复
热议问题