Feature Request - Create temporary table if it doesn't exist.

4

Hi,

Problem statement: Currently if we're running a query with multiple temporary tables and want to rerun the whole query without errors, it is necessary to drop the temporary tables. Usually you want to avoid having drop table statements when moving from dev to prod, inferring some manual effort depending on the query.

When using tools like Datagrip this is not a huge issue since it allows you to run specific parts of the query. But the nature of BI and analytics work means that you do not always have the luxury to run all your queries in a proper SQL IDE.

Proposed solution: Implement a new feature that would look like CREATE TEMP TABLE IF NOT EXISTS. This statement would only create a temporary table if it does not exist. Similar to https://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_TABLE_NEW.html.

This is a seemingly small feature that I believe would significantly enhance the user experience when writing or debugging large queries with many temporary tables.

asked 10 months ago211 views
1 Answer
1

Hello,

I notice that this is a post for a feature request but I do see something that could help in the mean time. To address this issue, you can consider using Common Table Expressions (CTEs) instead of temporary tables. CTEs allow you to create a temporary result set within a query, without the need to explicitly create and drop physical tables. This can make your queries more portable and easier to manage.

Here is an example of how you can use CTEs in Amazon Redshift:

-- Common Table Expression 1 (CTE1)
WITH cte1 AS (
  SELECT column1, column2
  FROM table1
  WHERE condition1
)

-- Common Table Expression 2 (CTE2)
, cte2 AS (
  SELECT column3, column4
  FROM table2
  WHERE condition2
)

-- Main Query using the CTEs
SELECT cte1.column1, cte1.column2, cte2.column3, cte2.column4
FROM cte1
JOIN cte2 ON cte1.column1 = cte2.column3;

By using CTEs, you avoid creating physical temporary tables and the associated "DROP TABLE" statements. Instead, the CTEs exist only within the scope of the main query, and they are automatically discarded after the query finishes execution.

However, do keep in mind that CTEs have some limitations. They are useful for temporary intermediate results, but if you need to use the same result set multiple times within a larger query or across different queries, temporary tables may still be necessary.

Ultimately, the choice between temporary tables and CTEs depends on the complexity of your queries and the specific use case at hand. In some cases, you might still need to use temporary tables, but in others, CTEs can be a cleaner and more portable solution.

profile pictureAWS
answered 9 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions