1

Snowflake doesn't accept T-sql but i'm trying to Create schema If it doesn't exists.

    IF NOT EXISTS (SELECT * FROM SYS.SCHEMA WHERE NAME = 'CBS')
    CREATE SCHEMA 'CBS';

How to do this in Snowflake?

peterb
  • 697
  • 3
  • 11
user1810575
  • 823
  • 3
  • 24
  • 45
  • Obviously you’ve read the Snowflake documentation so can you show what you’ve tried and what issue you are facing? – NickW Aug 24 '21 at 22:34

2 Answers2

2

Just use the 'if not exists' syntax:

Create schema if not exists 'cbs';
peterb
  • 697
  • 3
  • 11
  • If the user's primary role lacks privileges to create schemas in the database, this code returns an error. It's therefore not a great way to check if a schema exists. – Clay Mar 08 '23 at 17:33
0

It looks like you're using Matillion ETL to do this in Snowflake, in which case you have two main choices:

  1. Use the GUI, under Environments / choose one / Right-click / Create Schema

Add schema via UI

This method will simply fail with an error message if the schema already exists

  1. Use an Orchestration SQL Script component, with the SQL that peterb suggested

enter image description here

You can parameterize the schema name in the SQL script if you want to avoid hardcoding.

53epo
  • 784
  • 5
  • 7