在FirebirdSql中,如何从过程返回异常消息 [英] In FirebirdSql, how to return exception message from procedure

查看:82
本文介绍了在FirebirdSql中,如何从过程返回异常消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在发生异常时从过程中返回错误消息.在SQL Server中,您将选择Error_Number()和Error_Message().我将如何在FirebirdSql中做到这一点

I want to return the error message from a procedure when an exception happens. In SQL Server you would select the Error_Number() and Error_Message(). How would I do it in FirebirdSql

SET TERM ^ ;

CREATE PROCEDURE sprocname
 ( id int ) 
RETURNS 
 ( gcode int, errmsg varchar(250) )
AS 
BEGIN
  gcode = 0;
  errmsg = '';
  -- do procedure code here


  WHEN ANY DO
  BEGIN 
    gcode = gdscode; -- ??
    errmsg = ??;
  END
  SUSPEND;
END^

SET TERM ; ^

推荐答案

不幸的是,您将需要在客户端执行此操作,因为当前无法在PSQL中获得此功能. Firebird跟踪器中有一个功能请求,它已为Firebird 4实现,预计将于2019年发布.

Unfortunately you will need to do that at the client side, as it is currently not possible to obtain this in PSQL. There is a feature request in the Firebird tracker, which has been implemented for Firebird 4, which is expected to be released in 2019.

请参见 Firebird 4 Alpha 1发行说明,部分系统功能RDB $ ERROR() (警告:链接可能在下一个alpha版本中断开):

See Firebird 4 Alpha 1 release notes, section System Function RDB$ERROR() (warning: link might break with the next alpha release):

函数RDB$ERROR()将PSQL错误上下文作为输入, 返回活动异常的特定上下文.其范围是 限于PSQL中异常处理块的上下文. 在异常处理块之外,RDB$ERROR始终包含 NULL.

The function RDB$ERROR() takes a PSQL error context as input and returns the specific context of the active exception. Its scope is confined to the context of the exception-handling block in PSQL. Outside the exception handling block, RDB$ERROR always contains NULL.

返回值的类型取决于上下文.

The type of the return value depends on the context.

语法规则

RDB$ERROR ( context )
context ::= { GDSCODE | SQLCODE | SQLSTATE | EXCEPTION | MESSAGE }

[..]

示例

BEGIN
  ...
WHEN ANY DO
  EXECUTE PROCEDURE P_LOG_EXCEPTION(RDB$ERROR(MESSAGE));
END

这篇关于在FirebirdSql中,如何从过程返回异常消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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