Sometimes it's useful to create a temporary table within a BigQuery stored procedure, in order to store an intermediate result.
However, I've run into an error due to a name collision between a temporary table in a sproc and one in the session calling it. For example the code
CREATE OR REPLACE PROCEDURE `example-project.example_dataset.example_sproc`()
BEGIN
CREATE TEMP TABLE example_table AS SELECT 'example_data' AS example_column;
-- Other commands using example_table would go here
DROP TABLE example_table
END;
CREATE TEMP TABLE example_table AS SELECT 'other_example_data' AS other_example_column;
CALL `example-project.example_dataset.example_sproc`();
fails with the following error.
Already Exists: Table example-project:_scriptca02ec717db5bb7157d6ca3097152631de389011.example_table at [example-project.example_dataset.example_sproc:2:3]
I want to be able to call sprocs without worrying about the names of temporary tables used internally to them. Is there a way to make the temporary tables created within a sproc have a scope that prevents them from being accessed outside it? Or is there a better way to do the whole thing?
In some cases I could avoid the temporary table by using a WITH
statement to create a CTE, which I believe would be local to the sproc. But there are performance differnces between CTEs and temporary tables, so I would like to have the option of using either.