Using SnowSQL

This topic describes how to use SnowSQL, including starting/stopping the client, using commands and variables within the client, and other general usage information.

In this Topic:

Executing Commands

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 example:

user#> !help

+------------+-------------------------------------------+-------------+--------------------------------------------------------------------------------------------+
| Command    | Use                                       | Aliases     | Description                                                                                |
|------------+-------------------------------------------+-------------+--------------------------------------------------------------------------------------------|
| !abort     | !abort <query id>                         |             | Abort a query                                                                              |
| !connect   | !connect <connection_name>                |             | Create a new connection                                                                    |
| !define    | !define <variable>=<value>                |             | Define a variable as the given value                                                       |
| !edit      | !edit <query>                             |             | Opens up a text editor. Useful for writing longer queries. Defaults to last query          |
| !exit      | !exit                                     | !disconnect | Drop the current connection                                                                |
| !help      | !help                                     | !helps, !h  | Show the client help.                                                                      |
| !options   | !options                                  | !opts       | Show all options and their values                                                          |
| !print     | !print <message>                          |             | Print given text                                                                           |
| !queries   | !queries help, <filter>=<value>, <filter> |             | Lists queries matching the specified filters. Write <!queries> help for a list of filters. |
| !quit      | !quit                                     | !q          | Drop all connections and quit SnowSQL                                                      |
| !rehash    | !rehash                                   |             | Refresh autocompletion                                                                     |
| !result    | !result <query id>                        |             | See the result of a query                                                                  |
| !set       | !set <option>=<value>                     |             | Set an option to the given value                                                           |
| !source    | !source <filename>, <url>                 | !load       | Execute given sql file                                                                     |
| !spool     | !spool <filename>, off                    |             | Turn on or off writing results to file                                                     |
| !system    | !system <system command>                  |             | Run a system command in the shell                                                          |
| !variables | !variables                                | !vars       | Show all variables and their values                                                        |
+------------+-------------------------------------------+-------------+--------------------------------------------------------------------------------------------+

For a detailed description of each command, see Commands Reference (in this topic).

Using Variables

You can use variables to store and reuse values in a Snowflake session. Variables enable you to use user-defined and database values in queries.

You can define variables for SnowSQL in several ways:

  • In the configuration file (before connecting to Snowflake)
  • Using connection parameters (while connecting to Snowflake)
  • Executing commands (on the command line in the Snowflake session)

In addition, you can substitute variables in a session and Snowflake provides built-in variables.

Defining Before Connecting (Configuration File)

To define variables before connecting to Snowflake, add the variables in the config configuration file:

  1. Open the config configuration file in a text editor. The default location of the file is:

    Linux/Mac OS:~/.snowsql/
    Windows:%USERPROFILE%\.snowsql\

    Note

    You can specify a different location by including the --config <path> connection parameter when starting SnowSQL.

  2. Add variables in the [variables] section:

    [variables]
    <variable_name>=<variable_value>
    

    Where:

    • <variable_name> is a string of alphanumeric characters (case-insensitive) representing the name of the variable.
    • <variable_value> is a string representing the value for the variable. If needed, the string can be enclosed by single or double quotes.

    For example:

    [variables]
    tablename=CENUSTRACKONE
    

Defining While Connecting (-D or --variable Connection Parameter)

To define variables while connecting to Snowflake, on the terminal command line, specify the -D or --variable connection parameters followed by the variable name and value, in the form of <variable_name>=<variable_value>.

For example:

$ snowsql ... -D tablename=CENUSTRACKONE --variable db_key=$DB_KEY

In the above example:

  • -D sets a variable named tablename to CENUSTRACKONE.
  • --variable assigns a Snowflake variable named db_key to the DB_KEY environment variable.

Defining in a Session (!define Command)

To define a variable after connecting to Snowflake, execute the !define command in the session.

For example:

user#> !define tablename=CENUSTRACKONE

Substituting in a Session

If the variable_substitution configuration option is enabled, defined variables can be referenced in SnowSQL with their values substituted into queries by using the &<variable> syntax. Variable references are case-insensitive.

For example:

user#> !define snowshell=bash

user#> !set variable_substitution=true

user#> select '&snowshell';

+--------+
| 'BASH' |
|--------|
| bash   |
+--------+

If variable_substitution is disabled, no variable substitution occurs.

For example:

user#> !define snowshell=bash

user#> !set variable_substitution=false

user#> select '&snowshell';

+--------------+
| '&SNOWSHELL' |
|--------------|
| &snowshell   |
+--------------+

In addition, if the variable_substitution option is enabled, but no variable is defined, SnowSQL displays an error.

To combine a variable with text, enclose the variable reference in curly braces.

For example:

user#> !define snowshell=bash

user#> !set variable_substitution=true

select '&{snowshell}_shell';

+--------------+
| 'BASH_SHELL' |
|--------------|
| bash_shell   |
+--------------+

To use an ampersand sign without using substitution, escape the ampersand sign with a second ampersand sign:

&&<variable>

For example:

user#> !set variable_substitution=true

user#> select '&notsubstitution';

Variable notsubstitution is not defined

user#> select '&&notsubstitution';

+--------------------+
| '&NOTSUBSTITUTION' |
|--------------------|
| &notsubstitution   |
+--------------------+

Note

There is currently no option to unset an option value, such as the variable_substitution option. To change the value for an option, run the !set command again with the desired value.

Built-in Variables

SnowSQL includes a set of built-in variables that return metadata about statements executed in the current user session.

Each of these variable names begins with two underscore characters (“__”).

__rowcount

Returns the number of rows affected by the most recent DML statement executed by the user.

user#> insert into a values(1),(2),(3);

+-------------------------+
| number of rows inserted |
|-------------------------|
|                       3 |
+-------------------------+
3 Row(s) produced. Time Elapsed: 0.950s

user#> !set variable_substitution=true

user#> select &__rowcount;

+---+
| 3 |
|---|
| 3 |
+---+
__sfqid

Returns the query ID for the most recent query executed by the user.

user#> !set variable_substitution=true

user#> select * from a;

user#> select '&__sfqid';

+----------------------------------------+
| 'A5F35B56-49A2-4437-BA0E-998496CE793E' |
|----------------------------------------|
| a5f35b56-49a2-4437-ba0e-998496ce793e   |
+----------------------------------------+

Using Auto-Complete

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 [up-arrow] and [down-arrow] 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 [up-arrow] key. Press the key repeatedly to scroll through the buffer.

History File

The interactive command line history file is named history and is located in ~/.snowsql/history.

Running Batch Scripts

You can run batch scripts in two ways:

  • Using connection parameters (while connecting to Snowflake)
  • Executing commands (on the command line in the Snowflake session)

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 example:

snowsql -a xy12345 -u jsmith -f /tmp/input_script.sql -o output_file=/tmp/output.csv -o quiet=true -o friendly=false -o header=false -o output_format=csv

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.

For example:

user#> !source example.sql

Exporting Data

Output query results to a file in a defined format using the following configuration options:

  • output_format=output_format
  • output_file=output_filename

To remove the splash text, header text, timing, and goodbye message from the output, also set the following options:

  • friendly=false
  • header=false
  • timing=false

As with all configuration options, you can set them using any of the following methods:

  • In the configuration file (before connecting to Snowflake).
  • Using the -o or --options connection parameter (while connecting to Snowflake).
  • Executing the !set command (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:

snowsql -c my_example_connection -d sales_db -s public -q 'select * from mytable limit 10' -o output_format=csv -o header=false -o timing=false -o friendly=false  > output_file.csv

Changing the SnowSQL Prompt Format

The SnowSQL prompt dynamically displays context information about the current session:

  • When you log into Snowflake, the prompt displays your user name, as well as the default warehouse, database, and schema (if defaults have been set).
  • If you use a USE command in the session to set or change the warehouse, database, or schema, the prompt changes to reflect the context.

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]).

The token affects the prompt going forward. You can change the order and color for each token, as well as the delimiters between tokens.

As with all configuration options, you can set the prompt using any of the following methods:

  • In the configuration file (before connecting to Snowflake).
  • Using the -o or --options connection parameter (while connecting to Snowflake).
  • Executing the !set command (on the command line in the Snowflake session).

Note

If you change the prompt using the connection parameter or directly on the command line, the change applies to the current session only. To persist the change in future sessions, set the option in the configuration file.

Supported Tokens

SnowSQL supports the following object types as tokens:

  • user
  • account
  • role
  • database
  • schema
  • warehouse

Default Prompt

The SnowSQL default prompt uses the following tokens and structure:

[user]#[warehouse]@[database].[schema]>

For example:

marie#DATALOAD@DEMO_DB.PUBLIC>

Prompt Example

Continuing the example above, the following !set command executed in the command line changes the token order to user, database and schema, then warehouse. It also changes the delimiter for each token to a period (.) and sets the tokens to use different colors:

marie#DATALOAD@DEMO_DB.PUBLIC> !set prompt_format=[#FF0000][user].[#00FF00][database].[schema].[#0000FF][warehouse]>

This example results in the following prompt for the session:

SnowSQL prompt format

Disconnecting from Snowflake and Stopping SnowSQL

SnowSQL provides separate commands for:

  • Exiting individual connections (i.e. sessions) without stopping SnowSQL.
  • Quitting SnowSQL, which also automatically terminates all connections.

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.

Exit Codes

There are several possible exit codes that are returned when SnowSQL quits/exits:

0:Everything ran smoothly.
1:Something went wrong with the client.
2:Something went wrong with the command line arguments.
3:SnowSQL could not contact the server.
4:SnowSQL could not communicate properly with the server.
5:The exit_on_error configuration option was set and SnowSQL exited because of an error.

Default Key Bindings

[Tab]
Accept the current auto-complete suggestion.
[CTRL]-d
Quit/stop SnowSQL.

Commands Reference

!abort

Aborts a query (specified by query ID). The query ID can be obtained from the History History tab page in the web interface.

For example:

user#> !abort 77589bd1-bcbf-4ec8-9ebc-6c949b00614d;

!connect

SnowSQL supports multiple sessions (i.e. connections) with !connect <connection_name>:

  • The connection parameters/options associated with connection_name are 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].

When connecting, the connection is added to your connection stack, and exiting will return you to your previous connection. Quitting will exit all of your connections and quit, no matter how many connections you have.

For example:

Configuration file:

[connections.my_example_connection]
...

Command line:

user#> !connect my_example_connection

!define

Sets a variable to a specified value, using the following format:

!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\_

!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

You must save before or while exiting the editor, or else any text that was entered/modified in the editor will not be saved.

!exit , !disconnect

Drops the current connection and quits SnowSQL if it is the last connection.

!help , !helps , !h

Displays the help for SnowSQL commands.

!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.

!print

Prints the specified text to the screen and any files you are currently spooling to.

For example:

user#> !print Include This Text

!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.

For example:

  • Return the 25 most recent queries that ran in this current session:

    !queries session
    
  • Return the 20 most recent queries run in the account:

    !queries amount=20
    
  • Return the 20 most recent queries run in the account that took more than 200 milliseconds to run:

    !queries amount=20 duration=200
    
  • Return the 25 most recent queries that ran in the specified warehouse:

    !queries warehouse=mywh
    

This command creates a variable for each query ID returned. Note that variable substitution must be enabled for you to use these variables. For example:

user#> !queries session

+-----+--------------------------------------+----------+-----------+----------+
| VAR | QUERY ID                             | SQL TEXT | STATUS    | DURATION |
|-----+--------------------------------------+----------+-----------+----------|
| &0  | acbd6778-c68c-4e79-a977-510b2d8c08f1 | select 1 | SUCCEEDED |       19 |
+-----+--------------------------------------+----------+-----------+----------+

user#> !result &0

+---+
| 1 |
|---|
| 1 |
+---+

user#> !result acbd6778-c68c-4e79-a977-510b2d8c08f1

+---+
| 1 |
|---|
| 1 |
+---+

!quit , !q (also [CTRL]-d)

Drops all connections and exits SnowSQL.

!rehash

Re-syncs the auto-complete tokens.

Normal use does not require re-syncing the auto-complete tokens. However, forcing an update to the server-side tokens could be useful in certain scenarios (e.g. if a new user-defined function is created in a different session).

!result

Returns the result of a completed query (specified by query ID). Query IDs can be obtained from the History History tab page in the web interface or using the !queries command.

If the query is still running, the command waits until the query completes.

For example:

user#> !result 77589bd1-bcbf-4ec8-9ebc-6c949b00614d;

!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.

For example:

user#> !options

+-----------------------+-------------------+
| Name                  | Value             |
|-----------------------+-------------------|
 ...
| rowset_size           | 1000              |
 ...
+-----------------------+-------------------+

user#> !set rowset_size=500

user#> !options

+-----------------------+-------------------+
| Name                  | Value             |
|-----------------------+-------------------|
 ...
| rowset_size           | 500               |
 ...
+-----------------------+-------------------+

user#> !set rowset_size=1000

user#> !options

+-----------------------+-------------------+
| Name                  | Value             |
|-----------------------+-------------------|
 ...
| rowset_size           | 1000              |
 ...
+-----------------------+-------------------+

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.

For example:

user#> !source example.sql

user#> !load /tmp/scripts/example.sql

user#> !load http://www.example.com/sql_text.sql

!spool

This command can be executed in two ways:

  • Enable spooling and write the results of all subsequent statements/queries to the specified file:

    !spool <file_name>

  • Turn off results spooling (if it is enabled):

    !spool off

For example:

user#> select 1 num;

+-----+
| NUM |
|-----|
|   1 |
+-----+

user#> !spool /tmp/spool_example

user#> select 2 num;

+---+
| 2 |
|---|
| 2 |
+---+

user#> !spool off

user#> select 3 num;

+---+
| 3 |
|---|
| 3 |
+---+

user#> !exit

Goodbye!

$ cat /tmp/spool_example

+---+
| 2 |
|---|
| 2 |
+---+

You can change the output format by first running the !set output_format=<format> command. The option supports the following values:

  • custom
  • expanded
  • fancy_grid
  • grid
  • html
  • latex
  • latex_booktabs
  • mediawiki
  • orgtbl
  • pipe
  • plain
  • psql
  • rst
  • simple
  • tsv

Recommended value: psql, fancy_grid, or grid

For example, to output in CSV format:

user#> !set output_format=csv

user#> !spool /tmp/spool_example

!system

Executes a shell command.

!system <command>

The following example runs the ls command in the user’s home directory:

user#> !system ls ~

!variables , !vars

Lists all current variables. Returns each <variable_name>=<variable_value> pair currently defined.

Once a variable is assigned, it cannot be deleted, but its value can be removed by specifying the variable name with no value. For example:

user#> !set variable_substitution=true

user#> !define SnowAlpha=_ALPHA_

user#> !variables

+------------------+---------+
| Name             | Value   |
|------------------+---------|
| snowalpha        | _ALPHA_ |
+------------------+---------+

user#> !define SnowAlpha

user#> !variables

+------------------+-------+
| Name             | Value |
|------------------+-------|
| snowalpha        |       |
+------------------+-------+

user#> !define snowalpha=456

user#> select &snowalpha;

+-----+
| 456 |
|-----|
| 456 |
+-----+

For more information about setting variables, see Using Variables (in this topic).

Troubleshooting

Error message: Variable is not defined

Cause:

In some cases, this can be caused by use of the ampersand character (variable substitution character) inside a CREATE FUNCTION command.

create function mask_bits(...)
    ...
    as
    $$
    var masked = (x & y);
    ...
    $$;
Solution:

There is no solution to this problem that will work in all situations. However, if you do not intend to use variable substitution, you can avoid the problem by turning it off. To turn off variable substitution, execute the following command:

!set variable_substitution=false;

For more information about variable substitution, see Using Variables.