Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can we use dynamic sql in snowflake scripting?

We make extensive use of dynamic SQL in Snowflake Stored Procedures for many reasons.

One of them being that we have several databases, each prefixed with the environment name. Like PROD_DB1, PROD_DB2, etc...

Our code is not specific to one environment so all of our stored procedures start by checking which environment they are executing in (looking at the prefix of CURRENT_DATABASE()). Then we adapt our table/view names accordingly when doing cross database stuff.

This is just one example of the various reasons for dynamic SQL.

Is there a way to do this sort of things in Snowflake SQL (as opposed to Stored procedures using javascript)?

like image 417
Eric Mamet Avatar asked Sep 11 '25 23:09

Eric Mamet


1 Answers

Is there a way to do this sort of things in Snowflake SQL (as opposed to Stored procedures using javascript)?

Yes, it is possible to build arbitrary query and execute it using with Snowflake Scripting. EXECUTE IMMEDIATE:

Executes a string that contains a SQL statement or a Snowflake Scripting statement.

EXECUTE IMMEDIATE '<string_literal>'
 [ USING (bind_variable_1 [, bind_variable_2 ...] ) ] ;

Related: Snowflake dynamic SQL: Equivalent of DBMS_SQL/sp_executesql, Dynamic SQL in a Snowflake SQL Stored Procedure

like image 136
Lukasz Szozda Avatar answered Sep 16 '25 08:09

Lukasz Szozda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!