CertSafari
    Snowflake SnowPro Advanced: Data Analyst (DAA-C01)· Lessons

    Domain 3 · Lesson 13/19

    Snowflake stored procedures: rights, exceptions and async child jobs

    Use SQL extensibility features.

    12 min read
    8% of exam
    8 sources
    Published 20 Oct 2026
    Docs as of 4 Oct 2026

    What you will be able to do

    • Decide when multi-step logic belongs in a stored procedure and run it with CALL
    • Predict what an owner’s rights or caller’s rights procedure can do, including with session variables
    • Write an EXCEPTION block that logs an error and either returns a value or lets the CALL fail
    • Run statements concurrently with ASYNC and collect them safely with AWAIT

    1.What a stored procedure is for

    A stored procedure holds procedural code that a single query cannot express: branching, looping, several statements in a row, and SQL built at run time. A typical example is a cleanup job that deletes rows older than a cutoff date from a dozen tables. You write the DELETEs once, take the date as a parameter, and every user then runs one command instead of remembering every table. A nightly job that builds a result set, saves it with CREATE TABLE AS and writes an audit row is the same kind of work. It is multi-step and has side effects, so it belongs in a procedure, not a function.

    You create a procedure with CREATE PROCEDURE and run it with a CALL statement, for example CALL myproc('table_a', 'table_b', 5);. A UDF is the opposite case, a value you use inside an expression. A procedure can return a single value or, where the handler language supports it, tabular data. Handlers can be written in Java, JavaScript, Python, Scala or Snowflake Scripting. Snowflake Scripting is Snowflake's SQL-based procedural language, and it is what LANGUAGE SQL selects in the SQL examples on this page. Its code sits in blocks (DECLARE, BEGIN … END) that hold variables, branching, loops and exception handlers. Snowflake Scripting and JavaScript handlers must be in-line.

    A Python-handler procedure that copies a limited number of rows between tablessql
    CREATE OR REPLACE PROCEDURE myproc(from_table STRING, to_table STRING, count INT)
      RETURNS STRING
      LANGUAGE PYTHON
      RUNTIME_VERSION = '3.12'
      PACKAGES = ('snowflake-snowpark-python')
      HANDLER = 'run'
    as
    $$
    def run(session, from_table, to_table, count):
      session.table(from_table).limit(count).write.save_as_table(to_table)
      return "SUCCESS"
    $$;

    Not every procedure has to be a permanent object. A temporary procedure (CREATE PROCEDURE with TEMP) lasts only for the session. An anonymous procedure is created and called in one statement, then dropped. Neither needs the CREATE PROCEDURE privilege, so analysts without that grant can still use procedural logic.

    Checkpoint 1 of 4· Fill the gap

    A nightly job deletes old rows from a dozen tables and writes an audit row, so it was built as the stored procedure myproc. Which keyword runs it?

     ?  myproc('table_a', 'table_b', 5);

    Sources12

    2.Owner’s rights versus caller’s rights

    Each procedure runs with exactly one rights model, never both at once. The creator picks it at creation time, and if nothing is specified the procedure gets owner’s rights. The owner can switch it later with ALTER PROCEDURE.

    The two rights models side by side
    AspectOwner’s rights (EXECUTE AS OWNER)Caller’s rights (EXECUTE AS CALLER)
    Default?YesNo
    Privileges usedMostly the privileges of the procedure’s ownerThe database privileges of the role that called it
    Main advantageDelegates specific admin tasks without granting broader privilegesCan access information about the caller and the caller’s session
    ExampleNurse can delete old medical_records rows through Doctor’s procedureNurse’s delete fails if Nurse cannot delete from medical_records directly

    Owner’s rights is a way to delegate. When the ANALYST role is granted USAGE on a default-rights procedure, a call from that role runs with the owner’s privileges. The procedure can delete or update tables the analyst cannot touch directly, but only in the ways its code allows. Caller’s rights works the other way: any statement the caller could not run outside the procedure fails inside it as well.

    Session variables are where the default surprises people. An owner’s rights procedure can neither read the caller’s SQL variables nor set new ones, and this holds even when the caller is the owner. A caller’s rights procedure can do both. So any procedure that relies on $ variables from the caller’s session needs EXECUTE AS CALLER.

    A caller’s rights Snowflake Scripting procedure reading a session variablesql
    CREATE OR REPLACE PROCEDURE use_sql_variable_proc()
    RETURNS NUMBER
    LANGUAGE SQL
    EXECUTE AS CALLER
    AS
    DECLARE
      sess_var_x_2 NUMBER;
    BEGIN
      sess_var_x_2 := 2 * $example_use_variable;
      RETURN sess_var_x_2;
    END;

    Checkpoint 2 of 4· Check yourself

    An admin wants the ANALYST role to purge old rows from medical_records. ANALYST must not get DELETE on the table itself. Which setup works?

    Sources34

    3.Handling exceptions in Snowflake Scripting

    A Snowflake Scripting block can catch errors in an EXCEPTION section placed after its BEGIN statements. If it does not catch them, the error passes up to the enclosing block. Each block can have at most one handler, but that handler can contain several WHEN clauses, which are checked from top to bottom; the first one that matches runs. Inner blocks are checked before outer ones. You can name built-in exceptions such as STATEMENT_ERROR (a failing statement) and EXPRESSION_ERROR (for example, a VARCHAR assigned to a FLOAT), or exceptions you declared yourself with a number and a message. WHEN OTHER THEN catches everything else.

    Declaring, raising and catching user-defined exceptionssql
    DECLARE
      result VARCHAR;
      exception_1 EXCEPTION (-20001, 'I caught the expected exception.');
      exception_2 EXCEPTION (-20002, 'Not the expected exception!');
    BEGIN
      result := 'If you see this, I did not catch any exception.';
      IF (TRUE) THEN
        RAISE exception_1;
      END IF;
      RETURN result;
    EXCEPTION
      WHEN exception_2 THEN
        RETURN SQLERRM;
      WHEN exception_1 THEN
        RETURN SQLERRM;
    END;

    Each WHEN clause is either EXIT or CONTINUE, and EXIT is the default. EXIT runs the handler and then leaves the block. CONTINUE runs the handler and carries on with the statement after the one that failed, which suits errors too minor to stop the run. Inside the handler, three built-in variables describe the error: SQLCODE (a 5-digit signed integer), SQLERRM (the message) and SQLSTATE (a 5-character ANSI-style code). They are what you write to an audit table. Any variable the handler uses must be declared in DECLARE or passed in as an argument, not created inside BEGIN … END.

    The choice that matters most is what the caller ends up seeing. In the example above, the handler ends in RETURN, so the CALL succeeds and returns the message, and the failure is swallowed. A monthly load that must log to load_audit and still fail the caller's CALL needs a different handler. It inserts SQLCODE, SQLERRM and SQLSTATE into the audit table, then raises the same exception again so the error reaches the caller.

    Checkpoint 3 of 4· Check yourself

    A block runs three statements. The second raises exception_1, which the block handles with WHEN exception_1 CONTINUE THEN. After the handler's statements finish, what runs next?

    Sources56

    4.Asynchronous child jobs with ASYNC and AWAIT

    By default, a procedure runs its statements, or child jobs, one at a time, and each waits for the previous one to finish. Putting the ASYNC keyword in front of a statement makes it an asynchronous child job, so independent work such as inserts into separate tables or calls to other procedures runs concurrently and the total run time can drop. AWAIT ALL blocks until every asynchronous job started before it has finished. AWAIT res waits for the one job behind a RESULTSET, and you cannot read that RESULTSET until its AWAIT returns. These statements are valid only inside a Snowflake Scripting block.

    Three inserts running concurrently, collected by AWAIT ALLsql
    CREATE OR REPLACE PROCEDURE test_async_child_job_inserts()
    RETURNS VARCHAR
    LANGUAGE SQL
    AS
    BEGIN
      CREATE OR REPLACE TABLE test_child_job_queries1 (col1 INT);
      ASYNC (INSERT INTO test_child_job_queries1(col1) VALUES(1));
      ASYNC (INSERT INTO test_child_job_queries1(col1) VALUES(2));
      ASYNC (INSERT INTO test_child_job_queries1(col1) VALUES(3));
      AWAIT ALL;
    END;

    A RESULTSET is a Snowflake Scripting variable that holds the result of a query. When the query is declared with ASYNC, it starts as a child job right away, and the variable stands for that running job. Name the variable in an AWAIT to wait for just that job. Only after the AWAIT returns can the procedure read the RESULTSET's results, for example by opening a cursor on it. The two inserts below each get their own RESULTSET, so both run concurrently and the procedure waits for each in turn.

    Assigning ASYNC queries to RESULTSET variables and awaiting each onesql
    LET insert_1 RESULTSET := ASYNC (INSERT INTO orders_q3_2024 SELECT :arg1, :arg2);
        LET insert_2 RESULTSET := ASYNC (INSERT INTO orders_q3_2024 SELECT :arg3, :arg4);
        AWAIT insert_1;
        AWAIT insert_2;

    ASYNC is not fire-and-forget. If a child job is still running when the procedure ends, Snowflake cancels it, so a procedure without its final AWAIT ALL can return SUCCESS while some inserts never complete. Errors also show up late. A failed child job is reported at its AWAIT, which then fails and stops the procedure, and SQLCODE, SQLERRM and SQLSTATE are only set from that point. After an AWAIT ALL covering several jobs, those variables describe the first one that failed.

    Checkpoint 4 of 4· Check yourself

    A procedure starts four ASYNC inserts and runs AWAIT ALL. Two of the inserts fail. What do SQLCODE, SQLERRM and SQLSTATE report once the AWAIT statement fails?

    Sources278

    Sources

    (8)

    Every claim above is drawn from one of these pages, quoted as it was written on the date shown.

    1. 1.
      “With a procedure, you can use branching, looping, and other programmatic constructs.”
      ↩︎ What a stored procedure is for
      “From a stored procedure, you can return a single value or (where supported with the handler language) tabular data.”
      ↩︎ What a stored procedure is for
      “Create an anonymous procedure that you call immediately, and which is dropped immediately.”
      ↩︎ What a stored procedure is for
      “This allows the stored procedure owner to delegate the power to perform specified operations to users who otherwise could not do so.”
      ↩︎ Checkpoint
    2. 2.
      “Declares one or more Snowflake Scripting variables, cursors, RESULTSETs, nested stored procedures, or exceptions.”
      ↩︎ What a stored procedure is for
      “You can use this keyword to run multiple child jobs concurrently, which can improve efficiency and reduce overall run time.”
      ↩︎ Asynchronous child jobs with ASYNC and AWAIT
      “Runs the query as an asynchronous child job.”
      ↩︎ Asynchronous child jobs with ASYNC and AWAIT
    3. 3.
      “The default is owner’s rights.”
      ↩︎ Owner’s rights versus caller’s rights
      “the stored procedure can perform that task even when called by a role that does not have privileges to perform that task directly”
      ↩︎ Owner’s rights versus caller’s rights
      “Any statement that the caller could not execute outside the stored procedure cannot be executed inside the stored procedure, either.”
      ↩︎ Owner’s rights versus caller’s rights
      “The owner can change the procedure from an owner’s rights stored procedure to a caller’s rights stored procedure”
      ↩︎ Owner’s rights versus caller’s rights
    4. 4.
      “When a stored procedure runs with owner’s rights, it can’t access SQL (or session) variables.”
      ↩︎ Owner’s rights versus caller’s rights
      “Both fail when using an owner’s rights stored procedure, even if the caller is the owner.”
      ↩︎ Exam trap 1
    5. 5.
      “You can explicitly handle an exception by catching it with an EXCEPTION clause”
      ↩︎ Handling exceptions in Snowflake Scripting
      “To handle all other exceptions that aren’t built-in or declared, use a WHEN OTHER THEN clause.”
      ↩︎ Handling exceptions in Snowflake Scripting
      “continues with the statement immediately following the one that caused the error”
      ↩︎ Handling exceptions in Snowflake Scripting
      “In an exception handler, if you need to raise the same exception again”
      ↩︎ Handling exceptions in Snowflake Scripting
      “The block runs the statements in the exception block and continues with the statement immediately following the one that caused the error.”
      ↩︎ Checkpoint
    6. 6.
      “Snowflake supports no more than one exception handler per block.”
      ↩︎ Handling exceptions in Snowflake Scripting
    7. 7.
      “The results of a RESULTSET can’t be accessed until AWAIT is run for the RESULTSET.”
      ↩︎ Asynchronous child jobs with ASYNC and AWAIT
    8. 8.
      “When the ASYNC keyword is specified for a query, the stored procedure can’t access the query results until an AWAIT statement returns the results.”
      ↩︎ Asynchronous child jobs with ASYNC and AWAIT
      “If an asynchronous child job fails, the AWAIT or AWAIT ALL statement associated with the asynchronous job fails with an error”
      ↩︎ Asynchronous child jobs with ASYNC and AWAIT
      “if the stored procedure runs a child job that is still running when the stored procedure completes, the child job is canceled automatically.”
      ↩︎ Exam trap 2
      “When an AWAIT ALL statement is associated with multiple asynchronous child jobs, these built-in variables capture information about the first failing asynchronous child job.”
      ↩︎ Checkpoint

    Continue to part 3 of 3

    Regular, secure and materialized views in Snowflake

    Spotted a mistake, or was something unclear? Tell us.