cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

CREATE TEMP TABLE

dkxxx-rc
New Contributor III

The Databricks assistant tells me (sometimes) that `CREATE TEMP TABLE` is a valid SQL operation.  And other sources (e.g., https://www.freecodecamp.org/news/sql-temp-table-how-to-create-a-temporary-sql-table/) say the same.

But in actual practice, this generates a syntax error.  And also, sometimes the Assistant tells me that it's not a valid operation, and I should use CREATE TEMP VIEW instead.  

But I don't want a view.  I want the data materialized.  Is there an option to actually do TEMP TABLE, or something like it?

1 ACCEPTED SOLUTION

Accepted Solutions

KaranamS
Contributor III

Hi @dkxxx-rc ,

You can try to create a temp view and cache it. Caching will materialize it in memory.

CREATE TEMP VIEW temp_view AS

SELECT * FROM some_table WHERE condition

CACHE TABLE temp_view

View solution in original post

3 REPLIES 3

KaranamS
Contributor III

Hi @dkxxx-rc ,

You can try to create a temp view and cache it. Caching will materialize it in memory.

CREATE TEMP VIEW temp_view AS

SELECT * FROM some_table WHERE condition

CACHE TABLE temp_view

ashraf1395
Honored Contributor

You can create temp tables in dlt pipelines as well

simply
@Dlt.table(name ="temp_table", temporary = True)

def temp_table():

return <any_query>

 

dkxxx-rc
New Contributor III

In addition to accepting KaranamS's answer, I will note a longer and useful discussion, with caveats, at https://community.databricks.com/t5/data-engineering/how-do-temp-views-actually-work/m-p/20137/highl....