使用 SnowSQL¶
本主题介绍如何使用 SnowSQL,包括启动/停止客户端、使用客户端内的命令和变量以及其他常规用法信息。
执行命令
In a Snowflake session, you can issue commands to take specific actions. All commands in SnowSQL start with an exclamation point (!), followed by the command name.
例如:
For a detailed description of each command, see 命令参考 (in this topic).
使用变量
您可以使用变量在 Snowflake 会话中存储和重用值。变量使您能够在查询中使用用户定义值和数据库值。
接下来的部分将介绍如何定义和使用变量:
定义变量
您可以通过多种方式为 SnowSQL 定义变量:
- 在连接之前定义变量(配置文件)
- Defining variables while connecting ( or command-line flag)
- Defining variables within a session ( command)
在连接之前定义变量(配置文件)
To define variables before connecting to Snowflake, add the variables in the config configuration file:
-
Open the SnowSQL configuration file (named
config) in a text editor. The default location of the file is:- Linux/macOS:
~/.snowsql/- Windows:
%USERPROFILE%\.snowsql\
Note
You can change the default location by specifying the
--config pathcommand-line flag when starting SnowSQL.
-
In the
[variables]section, define any variables that you plan to use:
其中:
variable_nameis a string of alphanumeric characters (case-insensitive) representing the name of the variable.variable_valueis a string representing the value for the variable. If needed, the string can be enclosed by single or double quotes.
例如:
Defining variables while connecting (-D or --variable command-line flag)¶
To define variables while connecting to Snowflake, on the terminal command line, specify the -D or --variable
command-line flag. For the argument to this flag, specify the variable name and value in the form of
variable_name=variable_value.
例如:
- Linux/macOS:
- Windows:
在上面的例子中:
-Dsets a variable namedtablenametoCENUSTRACKONE.--variableassigns a Snowflake variable nameddb_keyto theDB_KEYenvironment variable.
Defining variables within a session (!define command)¶
To define a variable after connecting to Snowflake, execute the !define command in the session.
例如:
启用变量替换
To enable SnowSQL to substitute values for the variables, you must set the variable_substitution configuration option to
true in one of the following ways:
-
To set this option before you start SnowSQL, open the SnowSQL configuration file in a text editor, and set this option in the
[options]section: -
To set this option when you start SnowSQL, specify the
-ocommand-line flag: -
To set this option when in a SnowSQL session, execute the
!setcommand in the session:Note
There is currently no option to unset an option value, such as the
variable_substitutionoption. If you need to disable variable substitution, execute the command!set variable_substitution=false.
替换会话中的变量
After you enable variable substitution, you can use variables in SQL statements.
To use a variable in a statement, use the &variable_name syntax. Note that variable names are case-insensitive. For
example:
If the variable_substitution option is not enabled, no variable substitution occurs. For example:
如果引用尚未定义的变量,SnowSQL 会显示错误。例如:
要将变量与文本组合在一起,请将变量引用括在花括号中。例如:
要在不使用替换的情况下使用 & 号,请使用第二个 & 号将 & 号进行转义:
&&variable
例如:
列出变量
To list variables, execute the !variables or !vars command in the session:
使用内置变量
SnowSQL 包含一组内置变量,这些变量返回有关在当前用户会话中执行的语句的元数据。
这些变量名称中的每一个都以两个下划线字符(“__”)开头。
__rowcount返回受用户最近执行的 DML 语句影响的行数。
__sfqid返回用户最近执行的查询的查询 ID。
使用自动补全
Various SQL functions, table names, and variables are stored in SnowSQL and are auto-completed in interactive mode. To select an auto-complete suggestion, press the Tab key. To choose a different
suggestion, use the ↑ and ↓ keys to highlight the desired option, and then press Tab.
To disable auto-complete interactively, set the auto_completion configuration option to False in the configuration file.
Viewing your command-line history¶
Your recent command-line history can be recalled by using the ↑ key. Press the key repeatedly to scroll through the buffer.
历史记录文件
The interactive command-line history file is named history and is located in ~/.snowsql/history.
运行批处理脚本
您可以通过两种方式运行批处理脚本:
- 使用连接参数(连接到 Snowflake 时)
- 执行命令(在 Snowflake 会话的命令行上)
Running while connecting (-f connection parameter)¶
To execute a SQL script while connecting to Snowflake, use the -f <input_filename> connection parameter.
An output file for the script can be specified using -o output_file=<output_filename>. In addition, you can use -o quiet=true to turn off the standard output and
-o friendly=false to turn off the startup and exit messages.
例如:
For more information about all connection parameters, see Connection parameters reference.
Running in a session (!source or !load command)¶
To run a SQL script after connecting to Snowflake, execute the !source (or !load) command in the session.
例如:
导出数据
Output query results to a file in a defined format using the following configuration options:
-
output_format=output_format -
output_file=output_filename
若要从输出中移除初始文本、标题文本、计时和告别消息,也请设置以下选项:
friendly=falseheader=falsetiming=false
与所有配置选项一样,您可以使用以下任一方法设置这些选项:
- 在配置文件中(连接到 Snowflake 之前)。
- Using the
-oor--optionsconnection parameter (while connecting to Snowflake). - Executing the
!setcommand (on the command line in the Snowflake session).
Note that consecutive queries are appended to the output file. Alternatively, to redirect query output to a file and overwrite the file with each new statement, use the greater-than sign (>) in a
script.
In the following example, SnowSQL connects to an account using a named connection and queries a table. The output is written to a CSV file named output_file.csv in the current local directory:
- Linux/macOS:
- Windows:
更改 SnowSQL 提示格式¶
SnowSQL 提示会动态显示有关当前会话的上下文信息:
- 登录 Snowflake 时,提示将显示您的用户名,以及默认仓库、数据库和架构(如果已设置默认值)。
- 如果在会话中使用 USE 命令来设置或更改仓库、数据库或架构,则提示将会更改以反映上下文。
You can control the appearance and structure of the prompt using the prompt_format configuration option and a Pygments token in brackets for each object type, in the form of [token]
(e.g. [user] or [warehouse]).
此令牌会影响之后的提示。可以更改每个令牌的顺序和颜色,以及令牌之间的分隔符。
与所有配置选项一样,您可以使用以下任一方法设置提示:
- 在配置文件中(连接到 Snowflake 之前)。
- Using the
-oor--optionsconnection parameter (while connecting to Snowflake). - Executing the
!setcommand (on the command line in the Snowflake session).
Note
如果使用连接参数或直接在命令行上更改提示,则更改仅适用于当前会话。若要在将来的会话中保留更改,请在配置文件中设置相应选项。
支持的令牌
SnowSQL 支持以下对象类型作为令牌:
useraccountroledatabaseschemawarehouse
默认提示
SnowSQL 的默认提示使用以下令牌和结构:
例如:
提示示例
Continuing the example above, the following !set command executed in the command line adds the role token and changes the token order
to user and role, database and schema, then warehouse. It
also changes the delimiter for each token to a period (.) and sets the tokens to use different colors:
此示例将导致会话中出现以下提示:
断开与 Snowflake 的连接并停止 SnowSQL¶
SnowSQL 为以下操作提供单独的命令:
- 离开单个连接(即会话)而不停止 SnowSQL。
- 退出 SnowSQL,这也会自动终止所有连接。
To exit a connection/session, use the !exit command (or its alias, !disconnect). You can then connect again using !connect <connection_name> if you can defined multiple
connections in the SnowSQL config file. Note that, if you only have one connection open, the !exit command also quits/stops SnowSQL.
To exit all connections and then quit/stop SnowSQL, use the !quit command (or its alias, !q). You can also type
CTRL + d on your keyboard.
离开代码
SnowSQL 退出/离开时会返回几种可能的离开代码:
0:一切正常。
1:客户端出了点问题。
2:Something went wrong with the command-line arguments.
3:SnowSQL 无法联系服务器。
4:SnowSQL 无法与服务器正常通信。
5:The
exit_on_errorconfiguration option was set and SnowSQL exited because of an error.
默认按键绑定
Tab接受当前的自动补全建议。
CTRL+d退出/停止 SnowSQL。
命令参考
!abort¶
Aborts a query (specified by query ID). The query ID can be obtained from the History page in the web interface.
例如:
!connect¶
SnowSQL supports multiple sessions (i.e. connections) with !connect <connection_name>:
- The connection parameters/options associated with
connection_nameare stored in the corresponding[connections.<connection_name>]section in the SnowSQL configuration file. - If a parameter/option is not specified in the
[connections.<connection_name>]section, the unspecified parameter will default to the parameters under[connections].
连接时,连接将添加到连接堆栈中,而离开时将返回到以前的连接。退出将离开您的所有连接并退出,无论您有多少连接。
例如:
配置文件:
命令行:
!define¶
使用以下格式将变量设置为指定值:
!define <variable_name>=<variable_value>
The name and value must be separated by a single = with no spaces. Valid characters that can be used in the variable are:
0-9a-zA-Z_
For more information on defining and using variables, see 使用变量.
!edit¶
Opens up the editor that was set using the editor connection parameter (if no editor was set, the default is vim). The command accepts a query as an argument. If no argument is passed,
it opens up the last query that was run.
Note
您必须在离开编辑器之前或之时进行保存,否则不会保存在编辑器中输入/修改的任何文本。
!exit , !disconnect¶
如果当前连接是最后一个连接,则断开当前连接并且退出 SnowSQL。
!help , !helps , !h¶
显示 SnowSQL 命令的帮助。
!options , !opts¶
Returns a list of all the SnowSQL configuration options and their currently-set values. These options can be set using the !set command in the current SnowSQL
session.
Note
These options can also be set in the configuration file for SnowSQL or as connector parameters in the command line when invoking SnowSQL.
!pause¶
暂停正在运行的查询。按回车键继续。
!print¶
将指定的文本打印到屏幕和当前假脱机到的任何文件。
例如:
!queries¶
Lists all queries that match the specified filters. The default filters are session and amount=25, which return the 25 most recent queries in the current session.
例如:
-
返回此当前会话中最近运行的 25 个查询:
-
返回账户中最近运行的 20 个查询:
-
返回账户中最近运行且耗时超过 200 毫秒的 20 个查询:
-
返回指定仓库中最近运行的 25 个查询:
此命令为返回的每个查询 ID 创建一个变量。请注意,必须启用变量替换才能使用这些变量。例如:
!quit , !q (also CTRL + d)¶
断开所有连接并离开 SnowSQL。
!rehash¶
重新同步自动补全令牌。
正常使用并不需要重新同步自动补全令牌。但是,在某些情况下(例如,如果在另一个会话中创建了新的用户定义函数),强制更新服务器端令牌可能很有用。
!result¶
Returns the result of a completed query (specified by query ID). Query IDs can be obtained from the History page in the web interface or using the
!queries command.
如果查询仍在运行,此命令将等待查询完成。
例如:
!set¶
Sets the specified SnowSQL configuration option to a given value using the form <option>=<value>.
Note that there is no option currently to unset an option value. To change the value for an option, run the !set command again with the desired value.
例如:
Important
Spaces are not allowed between an option and its value. Some options support a defined set of values; SnowSQL returns an error if the provided value is unsupported. You cannot create new options.
For a list of all the configuration options you can set, use the !options command.
!source , !load¶
Executes SQL from a file. You can SQL from local files or a URL.
例如:
!spool¶
可以通过两种方式执行此命令:
-
启用假脱机,并将所有后续语句/查询的结果写入指定文件:
!spool <file_name> -
关闭结果假脱机(如果已启用):
!spool off
例如:
You can change the output format by first running the !set output_format=<format> command. The option supports the following values:
expandedfancy_gridgridhtmllatexlatex_booktabsmediawikiorgtblpipeplainpsqlrstsimpletsv
Recommended value: psql, fancy_grid, or grid
例如,要以 CSV 格式输出:
!system¶
执行 shell 命令。
!system <command>
The following example runs the ls command in the user’s home directory:
!variables , !vars¶
Lists all current variables. Returns each <variable_name>=<variable_value> pair currently defined.
变量在分配后无法删除,但可以通过指定不带值的变量名来移除变量值。例如:
For more information about setting variables, see 使用变量 (in this topic).
故障排除
Error Message: Variable is not defined¶
- Cause:
If you see this error message when running commands in SnowSQL, the cause might be an ampersand (
&) inside a CREATE FUNCTION command. (The ampersand is the SnowSQL variable substitution character.) For example, executing the following in SnowSQL causes this error:The error occurs when the function is created, not when the function is called.
- Solution:
If you do not intend to use variable substitution in SnowSQL, you can explicitly disable variable substitution by executing the following command:
For more information about variable substitution, see Using variables.
