snow sql¶
Executes Snowflake query. Use either query, filename or input option. Query to execute can be specified using query option, filename option (all queries from file will be executed) or via stdin by piping output from other command. For example cat my.sql | snow sql -i. The command supports variable substitution that happens on client-side.
语法
实参
无
选项
--query, -q TEXT要执行的查询。
--filename, -f FILE要执行的文件。默认值:[]。
--stdin, -i从标准输入读取查询。在管道输入此命令时使用它。默认值:False。
--variable, -D TEXTkey=value 格式的字符串。如果提供,SQL 内容将被视为模板并使用所提供的数据渲染。
--retain-comments保留传递给 Snowflake 的查询中的注释。默认值:False。
--single-transaction / --no-single-transaction连接时禁用自动提交。将语句放入 BEGIN/COMMIT 内可将语句作为单个事务执行,并确保所有命令成功完成或不应用更改。默认值:False。
--enable-templating [LEGACY%STANDARD%JINJA%ALL%NONE]Syntax used to resolve variables before passing queries to Snowflake. Default: [<_EnabledTemplating.LEGACY: ‘LEGACY’>, <_EnabledTemplating.STANDARD: ‘STANDARD’>].
-p, --project TEXTSnowflake 项目的存储路径。默认为当前工作目录。
--env TEXT格式为 key=value 的字符串。替换用于模板的 env 部分的变量。默认值:[]。
--connection, -c, --environment TEXTName of the connection, as defined in your config.toml file. Default: default.
--host TEXT连接的主机地址。替换为连接指定的值。
--port INTEGER连接的端口。替换为连接指定的值。
--account, --accountname TEXT分配给 Snowflake 账户的名称。替换为连接指定的值。
--user, --username TEXT连接到 Snowflake 的用户名。替换为连接指定的值。
--password TEXTSnowflake 密码。替换为连接指定的值。
--authenticator TEXTSnowflake 身份验证器。替换为连接指定的值。
--workload-identity-provider TEXT工作负载身份提供商(AWS、AZURE、GCP、OIDC)。替换为连接指定的值。
--private-key-file, --private-key-path TEXTSnowflake 私钥文件路径。替换为连接指定的值。
--token TEXT连接到 Snowflake 时使用的 OAuth 令牌。
--token-file-path TEXT连接到 Snowflake 时使用的带有 OAuth 令牌的文件路径。
--database, --dbname TEXT要使用的数据库。替换为连接指定的值。
--schema, --schemaname TEXT要使用的数据库架构。替换为连接指定的值。
--role, --rolename TEXT要使用的角色。替换为连接指定的值。
--warehouse TEXT要使用的仓库。替换为连接指定的值。
--temporary-connection, -xUses a connection defined with command-line parameters, instead of one defined in config. Default: False.
--mfa-passcode TEXT用于多重身份验证的令牌 (MFA)。
--enable-diag是否生成连接诊断报告。默认值:False。
--diag-log-path TEXTPath for the generated report. Defaults to system temporary directory. Default: <system_temporary_directory>.
--diag-allowlist-path TEXT包含允许列表参数的 JSON 文件的路径。
--oauth-client-id TEXT身份提供商为 Snowflake 集成提供的客户端 ID 的值。
--oauth-client-secret TEXT身份提供商为 Snowflake 集成提供的客户端密钥的值。
--oauth-authorization-url TEXT向驱动程序提供授权码的身份提供商端点。
--oauth-token-request-url TEXT向驱动程序提供访问令牌的身份提供商端点。
--oauth-redirect-uri TEXTURI 用于授权码重定向。
--oauth-scope TEXT身份提供商授权请求中请求的范围。
--oauth-disable-pkceDisables Proof Key for Code Exchange (PKCE). Default: False.
--oauth-enable-refresh-tokensEnables a silent re-authentication when the actual access token becomes outdated. Default: False.
--oauth-enable-single-use-refresh-tokensWhether to opt-in to single-use refresh token semantics. Default: False.
--client-store-temporary-credential存储临时凭据。
--format [TABLE%JSON%JSON_EXT|CSV]指定输出格式。默认:TABLE。
--verbose, -vDisplays log entries for log levels info and higher. Default: False.
--debugDisplays log entries for log levels debug and higher; debug logs contain additional information. Default: False.
--silent关闭到控制台的中间输出。默认值:False。
--enhanced-exit-codes根据错误类型区分退出错误代码。默认值:False。
--decimal-precision INTEGERNumber of decimal places to display for decimal values. Uses Python’s default precision if not specified. [env var: SNOWFLAKE_DECIMAL_PRECISION].
--helpDisplays the help text for this command.
使用说明
您可以使用以下选项之一指定要执行的 SQL 查询:
-
Specify the query string using the
--queryoption. -
Use the
--filenameoption to execute one or more files containing a SQL query or queries. When you specify multiple files, all files are executed sequentially on a single connection. For example:snow sql -f myfile.sqlsnow sql -f file1.sql -f file2.sql -f file3.sql
-
Specify the query as
stdinand pipe it to thesnow sqlcommand, such ascat my.sql | snow sql. -
If your query contains special characters, such as the dollar sign in SYSTEM functions, that you do not want the shell to interpret, you can do either of the following:
-
将查询放到单引号而不是双引号里,如:
snow sql -q 'SELECT SYSTEM$CLIENT_VERSION_INFO()' -
转义特殊字符,如:
snow sql -q "SELECT SYSTEM\$CLIENT_VERSION_INFO()"
-
-
Use variables for templating SQL queries with a combination of a
<% variable_name %>placeholder in your SQL queries and a-Dcommand-line option, in the form:Note
You can currently use the SnowSQL
&variable_nameand<% variable_name %>syntax for templates. However, Snowflake recommends using the<% variable_name %>syntax. -
在查询中指定脚本块。例如:
Note
When specifying the scripting block directly on the Snowflake CLI command line, the
$$delimiters might not work for some shells because they interpret that delimiter as something else. For example, the bash and zsh shells interpret it as the process ID (PID). To address this limitation, you can use the following alternatives:- If you still want to specify the scripting block on the command line, you can escape the
$$delimiters, as in\$\$. - You can also put the scripting block with the default
$$delimiters into a separate file and call it with thesnow sql -f filenamecommand.
- If you still want to specify the scripting block on the command line, you can escape the
设置 JSON 输出格式¶
The --format option provides two ways to display JSON:
-
JSON: Returns JSON as quoted strings, similar to the following: -
JSON_EXT: Returns JSON as JSON objects, similar to the following:
增强的错误代码
The --enhanced-exit-codes option provides information that helps identify whether problems result from query execution or from invalid command options. With this option, the snow sql command provides the following return codes:
0: Successful execution2: Command parameter issues5: Query execution issues1: Other types of issues
After the command executes, you can use the echo $? shell command to see the return code.
In this example, the command contains both a query parameter (-q 'select 1') and a query file parameter (-f my.query), which is an invalid parameter combination:
The following examples show the effect of the --enhanced-exit-codes option when the command contains an invalid query (slect is misspelled):
-
With the
--enhanced-exit-codesoption, the command returns a5exit code to indicate a query error: -
Without the
--enhanced-exit-codesoption, the command returns a1exit code to indicate a generic (other) error:
Alternatively, you can set the SNOWFLAKE_ENHANCED_EXIT_CODES environment variable to 1 to send the enhanced return codes for all snow sql commands.
交互模式
The snow sql command supports an interactive mode that lets you enter SQL commands one at a time. Interactive mode provides the following features:
-
语法突出显示

-
输入时代码补全

-
可搜索的历史记录
To search your command history, press
CTRL-R:
-
多行输入
Pressing
ENTERon a line that does not end with a semicolon (;) moves the cursor to the next line for more commands until a statement ends with a semi-colon.
To use interactive mode, enter the snow sql command followed by ENTER, as shown:
The command opens a sub-shell with a > prompt where you can enter SQL commands interactively:
然后,您可以输入 SQL 命令,如下所示:
Note
You must end each SQL statement with a semicolon (;).
To exit interactive mode, enter exit, quit, or CTRL-D.
在单个事务中使用多个命令
The --single-transaction option lets you enter multiple SQL commands to execute as an all-or-nothing set of commands.
By executing commands in a single transaction, you can ensure that all of the commands complete successfully before committing any of the changes.
If any of the commands fail, none of the changes from the successful commands persist.
以下示例展示了事务执行成功和失败的情况:
-
命令执行成功
然后,您可以验证命令是否已提交到数据库中:
-
单个事务失败
然后,您可以验证命令是否未提交到数据库中:
示例
-
The following example uses the SQL SYSTEM$CLIENT_VERSION_INFO system function to return version information about the clients and drivers.
-
下面的示例展示了如何使用客户端变量指定数据库:
When executed, the command substitutes the value
devin the<% database %>variable to create thedev.logsidentifier and then sends theselect * from dev.logsSQL query to Snowflake for processing.Note
You can currently use the SnowSQL
&variable_nameand &\{ variable_name \}syntax for templates. However, Snowflake recommends using the<% variable_name %>syntax. -
This example shows how to pass in environment variables using the
--envoption: -
By default, Snowflake CLI removes comments in SQL query from the output. The following example uses the
--retain-commentsoption to include the comments in the query results.Assume the
example.sqlfile contains the following statements and comment:When you execute the following command,
-- My commentappears in the query results.