Home > Software design >  snowflake: create or replace table when only a condition is met
snowflake: create or replace table when only a condition is met

Time:08-07

I have a requirement to create or replace a table only if certain variable value is 1

SET variable = (select statement) -- $variable will be 1 or 0 depending on value returned from select statment

create or replace table2 as select * from table1 (only if $variable =1)

Is there any way to do it? If the variable value is 0, the create statement should be skipped

CodePudding user response:

Using Snowflake Scripting block:

-- CREATE TABLE table1 AS SELECT 1 AS col;

SET variable = (SELECT 1);

BEGIN
   IF ($variable = 1) THEN 
      create or replace table table2 as select * from table1;
   END IF;
END;

Variable could be defined also at Snowflake Scripting block level:

DECLARE
   variable INTEGER := (SELECT 1);
BEGIN
   IF (:variable = 1) THEN 
      create or replace table table2 as select * from table1;
   END IF;
END;
  • Related