Object reference · Lesson 5 of 8Detailed study · about 12 min

Functions, UDFs and stored procedures

A function computes a result from inputs. A user-defined function adds your own reusable calculation. A stored procedure packages a sequence of operations that you invoke with CALL.

Understand the idea

A function takes inputs and produces an output according to its definition. A scalar function returns one value for each evaluated input row. Built-in functions avoid writing common calculations from scratch. Table functions are another category and return rows.

Key ideas in simple English
ConceptWhat it means
Built-in functionSnowflake-provided behavior, such as UPPER or ROUND.
UDFYour own function, called within supported SQL expressions.
Stored procedureA named routine for a workflow, invoked with CALL.

A concrete example

Northstar uses UPPER to standardize a label, a UDF to calculate an amount after tax, and a procedure for a multi-step maintenance workflow.

Remember: Use a function for a reusable calculation; consider a procedure for coordinated actions.

Explain it step by step · full theory

Understand the idea

A function takes inputs and produces an output according to its definition. A scalar function returns one value for each evaluated input row. Built-in functions avoid writing common calculations from scratch. Table functions are another category and return rows.

A UDF is a function you define. A stored procedure is a routine that can perform operations such as SQL statements and control flow, subject to its language and execution-rights model. A procedure can return a result, but it is not generally substituted for a scalar function inside SELECT.

Follow the example

This SQL UDF multiplies a supplied amount by 1.10 and rounds to two decimals. The tax rate is fictional and deliberately fixed for the example. Creating it needs CREATE FUNCTION on the lab schema; invoking it requires the relevant function privileges.

The SELECT returns a numeric amount of 110.00. It does not update ORDERS. When designing a real routine, define NULL behavior, numeric precision and permissions. For procedures, understand owner-rights versus caller-rights execution before allowing changes to data.

TOTAL is numerically 110.00. This is a calculation only; no table rows change.

Words used in this lesson
Built-in function
Snowflake-provided behavior, such as UPPER or ROUND.
UDF
Your own function, called within supported SQL expressions.
Stored procedure
A named routine for a workflow, invoked with CALL.

See how it works

Choose a step to explore the idea at your own pace.

Open the interactive explanation
Interactive explainer1 / 3

Functions, UDFs and stored procedures

Northstar sales team
Business requirement

Start with the requirement

Northstar uses UPPER to standardize a label, a UDF to calculate an amount after tax, and a procedure for a multi-step maintenance workflow.

Automatic playback is off for reduced motion. Select any step or use Previous and Next. Illustrations explain behavior; they are not live measurements.

Read the complete diagram explanation
  1. Start with the requirement

    Northstar uses UPPER to standardize a label, a UDF to calculate an amount after tax, and a procedure for a multi-step maintenance workflow.

  2. Choose the objects

    Compare the responsibilities: Built-in function: Snowflake-provided behavior, such as UPPER or ROUND.; UDF: Your own function, called within supported SQL expressions.; Stored procedure: A named routine for a workflow, invoked with CALL.

  3. Explain the outcome

    Use a function for a reusable calculation; consider a procedure for coordinated actions.

Try the SQL or command

Read → predict → run in your learning environment → compare. You can also learn by reading without an account.

New to SQL? Where to run it and how to read it

A query is a question written in SQL. Snowflake runs it and returns a result table. This website shows examples; it does not connect to your account.

  1. No account yet? Read and predict the output first. Follow the account and Snowsight lesson when you are ready.
  2. For SQL practice, use a learning account in Snowsight and select an authorized role and warehouse. A role determines permissions; a warehouse supplies processing power. Follow the example’s database/schema setup when it refers to stored objects.
  3. Read the entire example, including comments beginning with --. Run the selected statement or labeled block in the stated order. Examples labeled Python, shell or HTTP belong in that client, not in a SQL editor.
  4. Compare values and row counts with the explained result. If an example deliberately causes an error, follow its rollback and cleanup steps before continuing.
SELECT / FROM
Choose the values to return / identify their input.
VALUES / COLUMN1
VALUES supplies tiny example rows. COLUMN1 means their first column, COLUMN2 their second.
AS / WITH
AS gives a result a readable name. WITH names a temporary query step within one statement.
NULL / ::
NULL means missing or unknown. The :: notation converts a value to a named data type.
Session
One connection and its settings. Keep related transaction steps in the same session.
Database.schema.table
A full address for stored data. It is separate from the warehouse that processes queries.
Help with common practice errors
  • No active warehouse: select a warehouse your role can use. Do not assume you need to create a new one.
  • Object missing or not authorized: check the full name, required setup and active role. Ask the account owner for access if needed.
  • Object already exists: setup may already have run. Read the current state or choose a fresh lab name consistently; do not add OR REPLACE blindly.
  • Different result: check whether an earlier INSERT or UPDATE changed the data. VALUES examples provide their own fresh input for each statement.

Before running this lesson

Examples are for reading first. Optional execution requires an authorized learning account, a selected warehouse for queries, and privileges on the named objects. Nothing runs on this website.

Read the example

SQL · example
CREATE FUNCTION L2_OBJECTS_LAB.SALES.AMOUNT_WITH_TAX(AMOUNT NUMBER(10,2))
RETURNS NUMBER(10,2) LANGUAGE SQL
AS $$ ROUND(AMOUNT * 1.10, 2) $$;
SELECT L2_OBJECTS_LAB.SALES.AMOUNT_WITH_TAX(100.00) AS TOTAL;

What to expect and why

TOTAL is numerically 110.00. This is a calculation only; no table rows change.

Check your understanding

Choose an answer, then check your reasoning. This is practice, not a scored test.

How do you invoke a stored procedure?

Try it yourself

Choose an object for a reusable tax calculation and another for a multi-step cleanup workflow.

Compare your reasoning

Use a UDF for the calculation when it must appear in SQL expressions. Use a stored procedure for the workflow and invoke it with CALL, with explicit attention to its execution rights.

Official sources and curriculum

Object reference · See module scope