0% found this document useful (0 votes)
34 views

Snowflake Scripting

The document describes how to write SQL scripts and stored procedures in Snowflake including creating databases and schemas, using roles and warehouses, writing procedures, calling procedures, branching, looping, variables, exception handling, and cursors.

Uploaded by

clouditlab9
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Snowflake Scripting

The document describes how to write SQL scripts and stored procedures in Snowflake including creating databases and schemas, using roles and warehouses, writing procedures, calling procedures, branching, looping, variables, exception handling, and cursors.

Uploaded by

clouditlab9
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

/* CREATE DATABASE AND SCHEMA*/

USE DATABASE LA_DB;


USE SCHEMA LA_SCHEMA;

/* Use acccountadmin role and WH */

USE ROLE ACCOUNTADMIN;

USE WAREHOUSE COMPUTE_WH;

-- Write SQLScript in Procedure

CREATE OR REPLACE PROCEDURE myprocedure()


RETURNS VARCHAR
LANGUAGE SQL
AS
$$
-- Snowflake Scripting code
DECLARE
radius_of_circle FLOAT;
area_of_circle FLOAT;
BEGIN
radius_of_circle := 3;
area_of_circle := pi() * radius_of_circle * radius_of_circle;
RETURN area_of_circle;
END;
$$
;

-- Call Procedure
call myprocedure();

-- Write SQLScript without Procedure

DECLARE
radius_of_circle FLOAT;
area_of_circle FLOAT;
BEGIN
radius_of_circle := 3;
area_of_circle := pi() * radius_of_circle * radius_of_circle;
RETURN area_of_circle;
END;

-- When using old console or SnowSQL add $$

EXECUTE IMMEDIATE $$
DECLARE
radius_of_circle FLOAT;
area_of_circle FLOAT;
BEGIN
radius_of_circle := 3;
area_of_circle := PI() * radius_of_circle * radius_of_circle;
RETURN area_of_circle;
END;
$$
;

-- Branching Construct

BEGIN
LET count := 1;
IF (count < 0) THEN
return 'negative value';
ELSEIF (count = 0) THEN
return 'zero';
ELSE
return 'positive value';
END IF;
END;

--Looping

FOR <counter_variable> IN [ REVERSE ] <start> TO <end> { DO | LOOP }


<statement>;
[ <statement>; ... ]
END { FOR | LOOP } [ <label> ] ;

-- Use variables to find last DML affected any row

BEGIN
CREATE OR REPLACE TABLE my_values(id integer);
-- Insert 3 rows into a table.
INSERT INTO my_values VALUES (1), (2), (3);
-- SQLROWCOUNT is not affected by statements
-- that are not DML statements (e.g. SELECT statements).
SELECT * from my_values;
-- Returns the number of rows affected by
-- the last DML statement (the INSERT statement).
RETURN SQLROWCOUNT;
-- RETURN SQLFOUND; true if the last DML statement affected one or more rows.
-- RETURN SQLNOTFOUND , true if the last DML statement affected zero rows.

END;

-- Result Set

-- Exception Handling

-- Cursor Handling

You might also like