Skip to content
Advertisement

How to return the count of a table created and dropped within a SQL stored procedure in Snowflake?

I am trying to use a variable to store the count in a temporary table created within a stored procedure in Snowflake so that I can include the value in the return statement. When I try to do a select count(*) from the table I get SQL compilation error: Object 'CDP_SMS.DOMAIN_CANONICAL.TEMP_DELTA_MANUFACTURER_ITEM' does not exist or not authorized.. If I try to use LET to create a variable I get the same error. If I use SET to create a session variable, it doesn’t immediately error but I am unable to access the session variable afterwards (I’d guess that session variables don’t work in stored procedures). Removing the temporary keyword from the create table statement does not help.

However, I am able to use the table in an update statement (lines 36-48) and it works fine. Is there a way to store the count of a table created and dropped within a stored procedure to use in the return statement? I suppose I could use the logic that creates the temp table in a subquery and directly get the count but I’d really prefer not to do that (this code is a simplified version of the query that creates the temp table and it is actually pretty unwieldy with multiple unions and joins).

Advertisement

Answer

Using INTO:

Related: Setting Variables to the Results of a SELECT Statement

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement