NULL (Snowflake Scripting)¶
NULL 可以用作“no-op”(无操作)语句。
备注
Using NULL as a statement is uncommon. NULL is usually used as a value, rather than as a statement.
As a value, NULL means "no value." For more information, see the Wikipedia article on SQL NULL (link removed).
When working with semi-structured data types, such as JSON, you might need to distinguish between NULL as an SQL value and NULL as a JSON value (also called "VARIANT NULL").
备注
本 Snowflake Scripting 结构仅在 Snowflake Scripting 区块 内有效。
语法¶
NULL;
使用说明¶
The NULL statement can be executed only inside Snowflake Scripting code.
如果没有更高级别的处理程序,异常处理程序中的 NULL 语句可以确保代码继续执行而不是中止。
A NULL statement in a branch does nothing; however, it communicates to the reader that the author of the code explicitly considered the condition for which the branch would execute. In other words, the NULL shows that the branch condition wasn't overlooked or accidentally omitted.
在使用 NULL 语句之前,请考虑替代方法。
例如,假设正在编写具有异常处理程序的存储过程。在大多数存储过程中,如果每个非异常代码路径都应返回一个值,则涉及异常处理程序的每个代码路径也应返回一个值。在这种情况下,请避免执行 NULL 语句。相反,请考虑显式返回 NULL、空结果集或错误指示器。
您还可以使用 CONTINUE 处理程序在异常块中执行语句,并继续执行紧随导致错误的语句之后的语句。有关更多信息,请参阅 Handling an exception in Snowflake Scripting。
Examples¶
以下代码在异常处理程序中使用 NULL 语句来确保异常被捕获(而不是传递给调用方),但不执行具体操作:
CREATE PROCEDURE null_as_statement()
RETURNS VARCHAR
LANGUAGE SQL
AS
$$
BEGIN
SELECT 1 / 0;
RETURN 'If you see this, the exception was not thrown/caught properly.';
EXCEPTION
WHEN OTHER THEN
NULL;
END;
$$
;
调用存储过程:
CALL null_as_statement();
+-------------------+
| NULL_AS_STATEMENT |
|-------------------|
| NULL |
+-------------------+
备注
The NULL value returned by the CALL statement isn't directly due to the NULL statement in the exception. Instead, the return value is NULL because the stored procedure didn't execute an explicit RETURN statement.
Snowflake 建议存储过程显式返回一个值,包括在异常处理程序的每个分支中。