SQL 变量

您可以在 Snowflake 的会话中定义和使用 SQL 变量。

概述

Snowflake 支持用户声明的 SQL 变量。它们具有许多用途,例如存储应用程序特定的环境设置。

变量标识符

SQL 变量使用不区分大小写的名称进行全局标识。

变量 DDL

Snowflake 提供了以下使用 SQL 变量的 DDL 命令:

初始化变量

您可以通过执行 SQL 语句 SET 或在连接到 Snowflake 时在连接字符串中设置变量的方式来设置变量。

字符串或二进制变量的大小限制为 256 字节。

使用 SQL 初始化会话中的变量

You can initialize variables in SQL using the SET command. The data type of the variable is derived from the data type of the result of the evaluated expression. The following examples initialize variables:

SET my_variable1 = 10;
SET my_variable2 = 'example';
Copy

您可以使用返回单个结果的查询来初始化变量。以下示例使用查询来初始化变量:

SET cust_last_name = (SELECT lname FROM customers WHERE customer_id=100);
SET timestamp_variable = (SELECT CURRENT_TIMESTAMP());
Copy

You can initialize multiple variables in the same statement, thereby reducing the number of round-trip communications with the server. The following examples initialize multiple variables:

SET (var1, var2, var3) = (10, 20, 30);
SET (current_user, current_warehouse) = ((SELECT CURRENT_USER()), (SELECT CURRENT_WAREHOUSE()));
Copy

在连接时设置变量

除了使用 SET 在会话中设置变量之外,在 Snowflake 中用于初始化会话的连接字符串中,您还可以将变量作为实参传递。如果连接字符串的规范是唯一可能的自定义工具,则此选项特别有用。

For example, using the Snowflake JDBC driver, you can set additional connection properties that are interpreted as parameters. The JDBC API requires SQL variables to be strings.

// Build connection properties
Properties properties = new Properties();

// Required connection properties
properties.put("user"    ,  "jsmith"      );
properties.put("password",  "mypassword");
properties.put("account" ,  "myaccount");

// Set some additional variables.
properties.put("$variable_1", "some example");
properties.put("$variable_2", "1"           );

// Create a new connection
String connectStr = "jdbc:snowflake://localhost:8080";

// Open a connection under the snowflake account and enable variable support
Connection con = DriverManager.getConnection(connectStr, properties);
Copy

在 SQL 中使用变量

可以在 Snowflake 中任何允许使用字面量常量的地方使用变量,文档中注明者除外。若要将它们从绑定值和列名中区分开来,所有变量都必须以 $ 符号为前缀。

例如:

SET (min, max)=(40, 70);

SELECT $min;

SELECT AVG(salary) FROM emp WHERE age BETWEEN $min AND $max;
Copy

备注

Because the $ sign is the prefix used to identify variables in SQL statements, it is treated as a special character when used in identifiers. Identifiers (database names, table names, column names, and so on) can't start with special characters unless the entire name is enclosed in double quotes. For more information, see 对象标识符.

Variables can also contain identifier names, such as table names. To use a variable as an identifier, you must wrap it inside IDENTIFIER() (for example, IDENTIFIER($my_variable)). Some examples are below:

SET my_table_name='table1';
Copy
CREATE TABLE IDENTIFIER($my_table_name) (i INTEGER);
INSERT INTO IDENTIFIER($my_table_name) (i) VALUES (42);
Copy
SELECT * FROM IDENTIFIER($my_table_name);
Copy
+----+
|  I |
|----|
| 42 |
+----+

在 FROM 子句的上下文中,可以将变量名称封装在 TABLE() 中,如下所示:

SELECT * FROM TABLE($my_table_name);
Copy
+----+
|  I |
|----|
| 42 |
+----+
DROP TABLE IDENTIFIER($my_table_name);
Copy

有关 IDENTIFIER() 的更多信息,请参阅 使用 IDENTIFIER() 语法将字面量和变量用作标识符

查看会话的变量

若要查看当前会话中定义的所有变量,请使用 SHOW VARIABLES 命令:

SET (min, max)=(40, 70);
Copy
+----------------------------------+
| status                           |
|----------------------------------|
| Statement executed successfully. |
+----------------------------------+
SHOW VARIABLES;
Copy
+----------------+-------------------------------+-------------------------------+------+-------+-------+---------+
|     session_id | created_on                    | updated_on                    | name | value | type  | comment |
|----------------+-------------------------------+-------------------------------+------+-------+-------+---------|
| 10363773891062 | 2024-06-28 10:09:57.990 -0700 | 2024-06-28 10:09:58.032 -0700 | MAX  | 70    | fixed |         |
| 10363773891062 | 2024-06-28 10:09:57.990 -0700 | 2024-06-28 10:09:58.021 -0700 | MIN  | 40    | fixed |         |
+----------------+-------------------------------+-------------------------------+------+-------+-------+---------+

会话变量函数

The following convenience functions are provided for manipulating session variables to support compatibility with other database systems and to issue SQL through tools that do not support the $ syntax for accessing variables. All of these functions accept and return session variable values as strings:

  • SYS_CONTEXT 和 SET_SYS_CONTEXT 中的限制

  • SESSION_CONTEXT 和 SET_SESSION_CONTEXT 中的限制

  • GETVARIABLE 和 SETVARIABLE 中的限制

以下是使用 GETVARIABLE 的示例。首先,使用 SET 命令定义变量:

SET var_artist_name = 'Jackson Browne';
Copy
+----------------------------------+
| status                           |
+----------------------------------+
| Statement executed successfully. |
+----------------------------------+

返回变量值:

SELECT GETVARIABLE('var_artist_name');
Copy

在此示例中,输出是 NULL,因为 Snowflake 存储的变量都是大写字母。

更新大小写:

SELECT GETVARIABLE('VAR_ARTIST_NAME');
Copy
+--------------------------------+
| GETVARIABLE('VAR_ARTIST_NAME') |
+--------------------------------+
| Jackson Browne                 |
+--------------------------------+

您可以在 WHERE 子句中使用变量名称,例如:

SELECT album_title
  FROM albums
  WHERE artist = $var_artist_name;
Copy

移除变量

SQL 变量是会话专用的。关闭 Snowflake 会话时,将删除会话期间创建的所有变量。这意味着任何人都无法访问已在其他会话中设置的用户定义变量,并且当会话关闭时,这些变量会过期。

In addition, variables can be explicitly dropped using the UNSET command.

例如:

UNSET my_variable;
Copy
语言: 中文