Skip to content
  • AT SNOWFLAKE
  • Industry solutions
  • Partner & Customer Value
  • Product & Technology
  • Strategy & Insights
Languages
  • Deutsch
  • 한국어
  • Español
  • Français
  • English
  • Italiano
  • 日本語
  • Português
  • Deutsch
  • 한국어
  • Español
  • Français
  • English
  • Italiano
  • 日本語
  • Português
  • AT SNOWFLAKE
  • Industry solutions
  • Partner & Customer Value
  • Product & Technology
  • Strategy & Insights
  • Deutsch
  • 한국어
  • Español
  • Français
  • English
  • Italiano
  • 日本語
  • Português
  • VISÃO GERAL
    • Por que Snowflake
    • Histórias de clientes
    • Parceiros
    • Serviços
  • SETORES
    • Publicidade, meios de comunicação e entretenimento
    • Serviços financeiros
    • Saúde e ciências da vida
    • Indústria
    • Setor público
    • Varejo e bens de consumo
    • Tecnologia
  • For Departments
    • Marketing
    • IT
  • SAIBA MAIS
    • Biblioteca de recursos
    • Desenvolvedores
    • Quickstarts
    • Documentação
    • Laboratórios práticos
    • Treinamento
    • Guias
    • Glossário
  • Connect
    • Blog
    • Community
    • Events
    • Webinars
    • Podcast
    • Support
    • Trending
  • VISÃO GERAL
    • Sobre a Snowflake
    • Investidores
    • Liderança e direção
    • Carreiras
    • Newsroom
    • Speakers Bureau
    • ESG
    • Empreendimentos de Snowflake
Author
Felipe Hoffa Felipe Hoffa
Share
Subscribe
maio 18, 2022

What You Need to Know About Scripting in SQL with Snowflake 

  • Nenhuma categoria
What You Need to Know About Scripting in SQL with Snowflake 

Snowflake Scripting is now generally available, allowing you to create scripts and stored procedures in SQL with Snowflake. This procedural language extends the SQL language with structures and control statements, such as conditional and loop statements, and makes it easy for Snowflake users (many of whom know and love SQL) to create stored procedures and translate existing SQL-based scripts.

During the preview, we’ve seen incredible adoption and usage of this feature. It’s easy to get started with Snowflake Scripting. In the tips below, we’ll run through some important concepts to know, and you’ll be off and running in no time. 

Understanding blocks

With this feature, you write your procedural code within a Snowflake Scripting block. You can use a block in the definition of a stored procedure, or, if you don’t want to use it in a stored procedure, you can create an anonymous block as a separate, standalone SQL statement. 

Returning tables

One key new functionality is the ability to return a table with Snowflake Scripting. This way, Snowflake users can not only use Snowflake Scripting to develop their ETL and data processing tasks, but it also simplifies the development or migrations of reporting procedures. Snowflake Scripting is deeply integrated with Snowflake’s SQL and can also be used and shared when building your data sharing or marketplace applications.

Working control flow statements

Snowflake Scripting supports branching constructs including IF and CASE statements. 

  • IF enables you to execute a set of statements if a condition is met. 
  • CASE works similarly to IF but is simpler when specifying multiple conditions.  

There are four types of loops: FOR, WHILE, REPEAT, and LOOP. 

  • FOR loops repeat a sequence of steps for a specified number of times or for each row in a result set. They can be either counter-based or cursor-based. 
  • WHILE loops only operate while a condition is true. The WHILE condition is tested immediately before the body of the loop and if the condition is false, the loop won’t execute (even once if it happens on the first iteration). 
  • REPEAT loops will continue until a condition is true. 
  • LOOP loops will keep executing until a command is given. 

You can use a cursor to iterate through query results one row at a time. To retrieve data from the results of a query, use a cursor. You can use a cursor in loops to iterate over the rows in the results.

Handling exceptions

Within a Snowflake Scripting block, you can raise an exception if an error occurs, and that exception will prevent the next lines of code from executing. You can also handle exceptions that occur in your Snowflake Scripting code. 

When an exception is raised, Snowflake Scripting attempts to find a handler. An exception handler can contain its own exception handler in case an exception occurs while handling another exception.

Visit Snowflake documentation to learn more about Snowflake Scripting, and check out some sample SQL scripts on Medium to get you started.

BONUS: Rajiv Gupta, a Snowflake data superhero, also has a series on Medium that includes more on branching constructs, loops, cursor results, and exception handling.  

Share

Related Content

  • Nenhuma categoria
mar 03, 2022

Accelerate Legacy EDW Migrations with Snowflake Scripting, Now in Public Preview

We’re excited to announce the availability of Snowflake Scripting, a new procedural language, in public preview. Snowflake Scripting extends the SQL language with structures and control statements, such as conditional…

Expand your knowledge
Read More

Queries in SQL

Explore SQL queries using Snowflake. Enhance your SQL skills and leverage Snowflake's capabilities for efficient data querying. Unlock the power of data today.

More
Read More

Accelerate Legacy EDW Migrations with Snowflake Scripting, Now in...

We are extending SQL language with structures and control statements, so that users can now translate existing SQL-based...

See how
Read More

Effective Media Buying Requires Deep Audience Insights

With the plethora of digital platforms and services available today, media buying depends on big data for audience insights to help marketers keep pace.

Have a look
Read More

AI Governance Best Practices

AI governance is critical to addressing the ethical, legal, and societal implications of AI systems. Here are five crucial best practices for AI governance.

Full Details
Read More
Snowflake Inc.
  • Plataforma
    • Data Cloud
    • Arquitetura
    • preços
    • Snowflake Marketplace
    • Segurança e Confiança
  • Soluções
    • Serviços financeiros
    • Publicidade, meios de comunicação e entretenimento
    • Varejo e bens de consumo
    • Saúde e ciências da vida
    • Analítica de Marketing
  • Recursos
    • Biblioteca de recursos
    • Webinars
    • Documentação
    • Comunidade
    • Compras
    • Legal
  • Explore
    • Notícias
    • Blog
    • Tendência
    • Guias
    • Desenvolvedores
  • About
    • Sobre a Snowflake
    • Investidores
    • Liderança e direção
    • Empreendimentos de Snowflake
    • Carreiras
    • contato

Thanks for signing up!

  • Privacy Notice
  • Site Terms
  • Cookie Settings
  • Do Not Share My Personal Information

© 2023 Snowflake Inc. All Rights Reserved |  If you’d rather not receive future emails from Snowflake, unsubscribe here or customize your communication preferences