0

我想在大查询脚本中使用 RAISE 关键字将自定义消息打印为异常错误。但是,以下命令行在 raise 命令中引发错误。但是,如果我删除 raise 命令,它工作正常。您能帮我如何提出自定义错误消息吗?另外,要了解更多关于RAISE [USING MESSAGE = message];.

BEGIN
SELECT 1/0; -- attempts to divide by zero
RAISE USING message = "divisible with zero is not allowed.";
EXCEPTION WHEN ERROR THEN
SELECT FORMAT("Hey, you. When you executed %s at %s, it caused an error: %s. Please don't do that.", @@error.statement_text, @@error.formatted_stack_trace, @@error.message);
END;
4

1 回答 1

1

RAISE 语句只能在 EXCEPTION 子句中使用。RAISE 语句将重新引发捕获的异常,并保留原始堆栈跟踪。

参考:https ://cloud.google.com/bigquery/docs/reference/standard-sql/scripting#raise

因此,如果您想使用自定义消息引发异常,它应该是:

BEGIN
SELECT 1/0; -- attempts to divide by zero
EXCEPTION WHEN ERROR THEN
RAISE USING message = FORMAT("Hey, you. When you executed %s at %s, it caused an error: %s. Please don't do that.", @@error.statement_text, @@error.formatted_stack_trace, @@error.message);
END;
于 2021-09-14T03:49:04.307 回答