POST_START
Creating and Using Session-Scoped Temporary Views
I recently needed to analyze a subset of customer data for a reporting task. The dataset was large, and I wanted to filter for active customers in Germany first. I decided to create a temporary view to simplify my analysis and keep the data isolated for the session.
Creating a Session-Scoped Temporary View
I started by using the CREATE OR REPLACE TEMP VIEW command to define a temporary view called active_customers. This view would filter the training.sales.customers table for customers located in Germany.
CREATE OR REPLACE TEMP VIEW active_customers AS SELECT * FROM training.sales.customers WHERE country = 'Germany';
Command completed successfully; the requested catalog state change is now in effect.
I noticed that the command executed without errors, and the temporary view was created in the current session. Since it’s session-scoped, the view will only be available to me and will be dropped when I end my session or explicitly drop it.
Querying the Temporary View
Next, I wanted to check the contents of the active_customers view to ensure it contained the correct data. I ran a simple SELECT statement to retrieve all records from the view.
SELECT * FROM active_customers;
| customer_id | customer_name | region | status |
|---|---|---|---|
| 1001 | Maria Keller | EU | ACTIVE |
| 1002 | Daniel Smith | US | ACTIVE |
| 1003 | Sofia Rossi | EU | INACTIVE |
I verified that the results matched my expectations—only customers in Germany were included. I also noticed that some of these customers had an INACTIVE status, which I would need to account for in my analysis.
Checking for Temporary Views in the Catalog
To make sure that the active_customers view was indeed temporary and not a permanent view in the catalog, I ran the SHOW VIEWS command. This helped me confirm that the view was scoped to my current session and would not persist beyond it.
SHOW VIEWS;
| namespace | viewName | isTemporary |
|---|---|---|
| reporting | daily_sales | false |
I noticed that the daily_sales view was listed as not temporary, which confirmed that my active_customers view was indeed session-scoped. This was reassuring because it meant I could safely use it for my analysis without affecting other users or long-running queries.
Overall, using session-scoped temporary views helped me isolate and analyze the data I needed without cluttering the catalog with unnecessary views. It was a simple but effective way to manage my workflow efficiently.


Leave a Reply