What you will be able to do
- Set and read a session's role, warehouse, database and schema with USE commands and context functions
- Define, use and drop SQL variables, and predict when they disappear
- Classify a parameter as account, session or object, and list the levels where each can be set
- Work out which value of a parameter applies, and confirm where it came from with SHOW PARAMETERS
1.Session context: role, warehouse, database and schema
A session is one connection's working state, and four settings define its context: the current role, warehouse, database and schema. Each has its own USE command. USE ROLE sets the primary role, USE WAREHOUSE sets the compute that runs your queries, and USE DATABASE and USE SCHEMA set the namespace. The schema you choose has to be in the session's current database.
The namespace context is why SELECT * FROM orders works without a database or schema prefix. Snowflake takes the database and schema from the session. If the session has no current database and schema, every object name must be fully qualified, and an unqualified name fails. A script that works in a worksheet can fail the same way when a scheduled job runs it in a fresh session with no context.
To check the context from SQL, use context functions instead of querying views. CURRENT_USER() and CURRENT_WAREHOUSE() return the session's values directly. CURRENT_ORGANIZATION_NAME does the same one level higher in the hierarchy.
Checkpoint 1 of 5· Check yourself
A script runs SELECT * FROM orders without problems in a worksheet where a database and schema are set. A scheduled job then runs the same script in a fresh session, and no USE DATABASE or USE SCHEMA has been run in that session. What happens?
Snowflake takes the namespace for an unqualified name from the current session's database and schema. A fresh session with neither set cannot resolve orders. You either fully qualify the name or run USE DATABASE and USE SCHEMA first.
“If a database and schema are not in use for the session, the namespace must be explicitly specified when performing any operations on the objects.”Source: docs.snowflake.com
2.SQL variables last only as long as the session
A SQL variable is a named value you define in a session with SET. You can also pass one in the connection string, which helps with tools where the connection string is the only thing you can customise. The variable's data type comes from the value you assign, and a query that returns a single result can be assigned too. To refer to a variable, prefix its name with $ so Snowflake can tell it apart from column names and bind values.
SET (min, max)=(40, 70);
SELECT $min;
SELECT AVG(salary) FROM emp WHERE age BETWEEN $min AND $max;A variable can go anywhere a literal constant is allowed. To use one as a table name or another identifier, wrap it in IDENTIFIER($my_table_name). String and binary variables are limited to 16KB. SHOW VARIABLES lists every variable in the current session, and UNSET drops one.
Variables are private to the session that set them. Another session, even one opened by the same user, cannot read them. When the session closes, all of its variables are dropped. So a SET my_threshold = 100; in a worksheet is visible only in that worksheet's session and disappears when the session ends. For a value that has to persist or be shared, use a table or a parameter.
Checkpoint 2 of 5· Check yourself
An analyst runs SET my_threshold = 100; in one worksheet. Later, the same user opens a second session and runs SELECT $my_threshold; while the first session is still open. What happens, and what should the analyst use instead if several sessions need the value?
SQL variables are private to the session that created them. No other session can read them, even one opened by the same user, and they are dropped when the session closes. A value that has to persist or be shared belongs in a table or a parameter.
“SQL variables are private to a session. When a Snowflake session is closed, all variables created during the session are dropped.”Source: docs.snowflake.com
Sources3
3.Three parameter types and the levels each can be set at
Parameters control how the account, sessions and objects behave. Every parameter has a default value. There are three types, and a parameter's type decides where you can override it.
| Type | Levels it can be set at | Commands |
|---|---|---|
| Account parameter | Account only | ALTER ACCOUNT |
| Session parameter | Account, then User, then Session | ALTER ACCOUNT, ALTER USER, ALTER SESSION |
| Object parameter | Account, then Object | ALTER ACCOUNT, CREATE <object> or ALTER <object> |
Most parameters are session parameters, such as DATE_OUTPUT_FORMAT, TIMEZONE and QUERY_TAG. An account administrator sets the account-wide default. An administrator can override it for one user with ALTER USER, and users can do the same for themselves. Any user can override it for the current session with ALTER SESSION.
Object parameters attach to particular object types. Here are some, with the objects each can be set on:
DATA_RETENTION_TIME_IN_DAYS: database, schema and table.PIPE_EXECUTION_PAUSED: schema and pipe.LOG_LEVEL: from the account down to individual stored procedures and functions.MAX_CONCURRENCY_LEVEL: warehouse.STATEMENT_TIMEOUT_IN_SECONDS: warehouse. It is also a session parameter, so it can be set on a warehouse and in a session.
Checkpoint 3 of 5· Match them up
Match each parameter type to the levels where it can be set and the commands that set it
Tap a term, then the definition that fits it.
A parameter's type decides where it can be overridden. Account parameters stop at the account. Session parameters go down through user to session. Object parameters go from the account to an individual object such as a warehouse, database, schema, table or pipe.
“Object parameters can be set at the account and object level.”Source: docs.snowflake.com
4.Precedence: the most specific level wins
When the same parameter is set at more than one level, the most specific setting wins. For a session parameter, the account value is the default for each user, the user value is the default for each session that user starts, and an ALTER SESSION value overrides both for the current session. In the example above the session's 300 seconds applies, and other users' sessions keep the account's 3600. NETWORK_POLICY works the same way: if it is set on both the account and a user, the user's policy wins.
Object parameters follow the same rule. An account-level value is the default for new objects. A value set in CREATE or ALTER on a particular object overrides the default for that object. For example, if the account's DATA_RETENTION_TIME_IN_DAYS is 1 and you run CREATE DATABASE sales DATA_RETENTION_TIME_IN_DAYS = 7, tables in sales get 7 unless a schema or table sets its own value, and tables in databases that set nothing still get 1.
SHOW PARAMETERS tells you where a value came from. The value column shows the current setting and the level column shows where it was set. An empty level means the parameter has never been set, so the default applies. The command's scope also matters. With no IN clause it lists only session parameters. To see object parameters, add an IN clause with an object type and name. To see everything, add IN ACCOUNT. If you use LIKE, it has to come before IN.
ALTER ACCOUNT SET DATE_OUTPUT_FORMAT = 'DD/MM/YYYY'; SHOW PARAMETERS LIKE 'DATE_OUTPUT%' IN ACCOUNT;Here the level column shows ACCOUNT and the value is DD/MM/YYYY. That value is only the starting point for sessions, though. A user or session can still override it. ALTER ACCOUNT UNSET DATE_OUTPUT_FORMAT restores the default.
Checkpoint 4 of 5· Check yourself
A security administrator assigns network policy corp_vpn to the account and assigns network policy contractor_ips to one contractor's user. When that contractor signs in, which policy applies?
When a parameter is set at more than one level, the most specific level wins. A user-level NETWORK_POLICY overrides the account-level one for that user. Other users still get the account's policy.
“If this parameter is set on the account and a user in the same account, the user-level network policy overrides the account-level network policy.”Source: docs.snowflake.com
Checkpoint 5 of 5· Fill the gap
After setting an account-wide default, you want SHOW PARAMETERS to search all parameters, including account and object parameters, not just session parameters. Which keyword completes the IN clause?
ALTER ACCOUNT SET DATE_OUTPUT_FORMAT = 'DD/MM/YYYY'; SHOW PARAMETERS LIKE 'DATE_OUTPUT%' IN ? ;IN ACCOUNT lists every parameter, including account and object parameters. IN SESSION (the default when you leave out IN) lists only session parameters. IN with an object type lists only that object's parameters.
Source: docs.snowflake.comPractise it for real
Create session variables, then find where parameter values come from
1.Run SET (min, max)=(40, 70); and then SELECT $min;
Why: Shows that a variable can be used anywhere a literal can, as long as it has the $ prefix.
You should see: The query returns 40.
2.Run SHOW VARIABLES;
Why: Lists the variables in the current session and the names Snowflake stored them under.
You should see: Two rows, MAX with 70 and MIN with 40, both stored in uppercase.
3.Run SHOW PARAMETERS LIKE '%time%';
Why: With no IN clause, the command lists only session parameters.
You should see: Session parameters whose names contain TIME. The level column is blank for any that have not been explicitly set.
4.Run SHOW PARAMETERS IN DATABASE mydb; using one of your own databases.
Why: Object parameters appear only when you name an object.
You should see: Object parameters such as DATA_RETENTION_TIME_IN_DAYS, each with its value and the level where it was set.
Stuck? Get a nudge
If the IN DATABASE output shows a level for DATA_RETENTION_TIME_IN_DAYS, check whether the value was set on the database itself or inherited from the account.
Sources
(6)
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.
“Specifies the schema to use in the session (specified schema must be in the current database for the session).”
↩︎ Session context: role, warehouse, database and schema“Specifies the primary role to use in the session.”
↩︎ Session context: role, warehouse, database and schema - 2.
“the namespace is inferred from the current database and schema in use for the session.”
↩︎ Session context: role, warehouse, database and schema“If a database and schema are not in use for the session, the namespace must be explicitly specified”
↩︎ Session context: role, warehouse, database and schema“If a database and schema are not in use for the session, the namespace must be explicitly specified when performing any operations on the objects.”
↩︎ Checkpoint - 3.
“To distinguish them from bind values and column names, all variables must be prefixed with a $ sign.”
↩︎ SQL variables last only as long as the session“To use a variable as an identifier, you must wrap it inside IDENTIFIER()”
↩︎ SQL variables last only as long as the session“The size of string or binary variables is limited to 16KB.”
↩︎ SQL variables last only as long as the session“SQL variables are private to a session. When a Snowflake session is closed, all variables created during the session are dropped.”
↩︎ Exam trap 1“In this example, the output is NULL because Snowflake stores variables with all uppercase letters.”
↩︎ Prediction“SQL variables are private to a session. When a Snowflake session is closed, all variables created during the session are dropped.”
↩︎ Checkpoint - 4.
“Session parameters can be set at the account, user, and session level.”
↩︎ Three parameter types and the levels each can be set at“Object parameters can be set at the account and object level.”
↩︎ Three parameter types and the levels each can be set at“If a parameter has been explicitly set, the output of this command also shows the level at which the parameter has been set.”
↩︎ Precedence: the most specific level wins - 5.
“You can only set account parameters at the account level”
↩︎ Three parameter types and the levels each can be set at“Also a session parameter (can be set at both the object and session levels).”
↩︎ Three parameter types and the levels each can be set at“The values that you set for a user become the default values in any session started by that user.”
↩︎ Precedence: the most specific level wins“Users can run the ALTER SESSION command to override session parameters for the current session.”
↩︎ Precedence: the most specific level wins“Users with the appropriate privileges can run the CREATE <object> or ALTER <object> commands to override object parameters for an individual object.”
↩︎ Precedence: the most specific level wins“By default, the command displays only session parameters:”
↩︎ Exam trap 2“If this parameter is set on the account and a user in the same account, the user-level network policy overrides the account-level network policy.”
↩︎ Exam trap 3“If this parameter is set on the account and a user in the same account, the user-level network policy overrides the account-level network policy.”
↩︎ Checkpoint - 6.
“If the level column is empty for a parameter, the parameter is not explicitly set and the current value is the default value.”
↩︎ Precedence: the most specific level wins“this date display format is only the default and can be overridden for any individual user or session.”
↩︎ Precedence: the most specific level wins