---
title: CREATE PROCEDURE
description: Create a Unity Catalog stored procedure that runs a set of SQL statements with CREATE PROCEDURE in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-syntax-ddl-create-procedure
---

# CREATE PROCEDURE

**Applies to:** ![check marked yes](https://docs.databricks.com/gcp/en/images/icons/check.png) Databricks SQL Databricks Runtime 17.0 and above ![check marked yes](https://docs.databricks.com/gcp/en/images/icons/check.png) Unity Catalog only

Creates a procedure in Unity Catalog that takes or modifies arguments, executes a set of SQL statements, and optionally returns a result set.

## Permissions

To create a procedure, you must have `USE CATALOG` on the catalog and `USE SCHEMA` on the schema where you create the procedure, and have `CREATE FUNCTION` privilege on the schema. Creating a procedure does not require privileges on objects referenced in its body.

When the procedure body runs, each statement requires its own privileges, checked against the authorized user inherited from the caller for `SQL SECURITY INVOKER` or the procedure owner for `SQL SECURITY DEFINER`.

Replacing an existing procedure with `CREATE OR REPLACE PROCEDURE` requires `USE CATALOG` on the catalog and `USE SCHEMA` and `CREATE FUNCTION` on the schema that contains the procedure. You must also own or have `MANAGE` on the procedure, its parent schema, or its parent catalog.

## Syntax

```
CREATE [OR REPLACE] PROCEDURE [IF NOT EXISTS]
    procedure_name ( [ procedure_parameter [, ...] ] )
    [ characteristic [...] ]
    AS compound_statement

procedure_parameter
    [ IN | OUT | INOUT ] parameter_name data_type
    [ DEFAULT default_expression ] [ COMMENT parameter_comment ]

characteristic
  { LANGUAGE SQL |
    SQL SECURITY { INVOKER | DEFINER } |
    NOT DETERMINISTIC |
    COMMENT procedure_comment |
    DEFAULT COLLATION default_collation_name |
    MODIFIES SQL DATA }
```

## Parameters

* **OR REPLACE**

  If specified, a procedure with the same name is replaced.
  You cannot specify this parameter with `IF NOT EXISTS`; specifying both raises [INVALID\_SQL\_SYNTAX.CREATE\_ROUTINE\_WITH\_IF\_NOT\_EXISTS\_AND\_REPLACE](https://docs.databricks.com/gcp/en/error-messages/invalid-sql-syntax-error-class.md#create_routine_with_if_not_exists_and_replace).

* **IF NOT EXISTS**

  If specified, creates the procedure only when a procedure with that name does not exist already.
  If a procedure with the same name exists, the statement is ignored.
  You cannot specify this parameter with `OR REPLACE`; specifying both raises [INVALID\_SQL\_SYNTAX.CREATE\_ROUTINE\_WITH\_IF\_NOT\_EXISTS\_AND\_REPLACE](https://docs.databricks.com/gcp/en/error-messages/invalid-sql-syntax-error-class.md#create_routine_with_if_not_exists_and_replace).

* **[procedure\_name](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-names.md#procedure-name)**

  A name for the procedure. You can optionally qualify the procedure name with a schema name.
  If the name is not qualified the permanent procedure is created in the current schema.

  The procedure name must be unique for all routines (procedures and functions) in the schema. If a routine with the same name exists and neither `OR REPLACE` nor `IF NOT EXISTS` is specified, Databricks raises [ROUTINE\_ALREADY\_EXISTS](https://docs.databricks.com/gcp/en/error-messages/error-classes.md#routine_already_exists).

* **procedure\_parameter**

  Specifies a parameter of the procedure.

  * **[parameter\_name](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-identifiers.md)**

    The parameter name must be unique within the procedure; otherwise Databricks raises [DUPLICATE\_ROUTINE\_PARAMETER\_NAMES](https://docs.databricks.com/gcp/en/error-messages/error-classes.md#duplicate_routine_parameter_names).

  * **IN**, **INOUT**, or **OUT**

    Optionally describes the mode of the parameter.

    * **IN**

      Defines an input only parameter. This is the default.

    * **INOUT**

      Defines a parameter that accepts an input-output argument.
      If the procedure completes without an unhandled error, it will return the final parameter value as an output.

    * **OUT**

      Defines an output parameter.
      The parameter is initialized to `NULL` and, if the procedure completes without an unhandled error, it will return the final parameter value as an output.

  * **[data\_type](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-datatypes.md)**

    Any supported data type.

  * **DEFAULT [default\_expression](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-expression.md)**

    An optional default to be used when a function invocation does not assign an argument to the parameter.
    `default_expression` must be [castable](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-datatype-rules.md) to `data_type`.
    The expression must not reference another parameter or contain a subquery.

    When you specify a default for one parameter, all following parameters must also have a default.

    `DEFAULT` is not supported for `OUT` or `INOUT` parameters; specifying one raises [PROCEDURE\_CREATION\_PARAMETER\_OUT\_INOUT\_WITH\_DEFAULT](https://docs.databricks.com/gcp/en/error-messages/error-classes.md#procedure_creation_parameter_out_inout_with_default).

  * **COMMENT comment**

    An optional description of the parameter. `comment` must be a `STRING` literal.

* **[compound\_statement](https://docs.databricks.com/gcp/en/sql/language-manual/control-flow/compound-stmt.md)**

  A SQL compound statement (`BEGIN ... END`) with the definition of the SQL Procedure.

  When the procedure is created syntactic correctness is validated.
  The procedure body is not validated for semantic correctness until the procedure is invoked.

* **characteristic**

  One of `SQL SECURITY INVOKER` or `SQL SECURITY DEFINER`, and `LANGUAGE SQL` are required.
  All others are optional.
  You can specify any number of characteristics in any order, but you can specify each clause only once.

  * **LANGUAGE SQL**

    The language of the function implementation.

  * **SQL SECURITY INVOKER**

    Specifies that any SQL statements in the body of the procedure run under
    the authority of the user invoking the procedure.

    When resolving relations and routines within the body of the procedure, Databricks uses the current catalog, the current schema, and the session [PATH](https://docs.databricks.com/gcp/en/sql/language-manual/parameters/path.md) at time of invocation.

    See [Authorized user and session user](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-authorized-user.md) for how the authorized user and the session user behave inside procedure bodies and across nested calls.

  * **SQL SECURITY DEFINER**

    **Applies to:** ![check marked yes](https://docs.databricks.com/gcp/en/images/icons/check.png) Databricks SQL

    Specifies that any SQL statements in the body of the procedure are always executed under
    the authority of the owner (definer) of the procedure, regardless of which user invokes
    the procedure. That is, the owner is the *authorized user* for the body. The invoker
    only requires the `EXECUTE` privilege on the procedure; all access checks on relations,
    routines, and other objects referenced from the body are evaluated against the
    authorized user.

    When resolving relations and routines within the body of the procedure, Databricks uses the catalog, schema, and [PATH](https://docs.databricks.com/gcp/en/sql/language-manual/parameters/path.md) that were current at the time the procedure was created. The captured path omits `system.session`, so the invoker's temporary views, temporary tables, session variables, and temporary functions are found only by qualified names, for example `session.object_name` or `system.session.object_name`. See [Captured path](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-name-resolution.md#function-frozen-path).

    SQL configurations (for example, `ANSI_MODE` or the default time zone) that affect the semantics of statements in the body are also captured at creation time and used during every invocation of the procedure, regardless of the invoker's session settings.

    Inside a `SQL SECURITY DEFINER` body, [current\_catalog](https://docs.databricks.com/gcp/en/sql/language-manual/functions/current_catalog.md) and [current\_schema](https://docs.databricks.com/gcp/en/sql/language-manual/functions/current_schema.md) return the catalog and schema that were current when the procedure was created. [current\_path](https://docs.databricks.com/gcp/en/sql/language-manual/functions/current_path.md) returns that captured path, without `system.session`. It does not return the invoker's session path.

    A nested `SQL SECURITY INVOKER` procedure called from a `SQL SECURITY DEFINER` body uses the outer procedure's frozen path, not the original invoker's session path.

    `SQL SECURITY DEFINER` does not change the value of [session\_user](https://docs.databricks.com/gcp/en/sql/language-manual/functions/session_user.md): it continues to return the user who issued the `CALL`. See [Authorized user and session user](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-authorized-user.md) for how the authorized user and the session user differ inside a `SQL SECURITY DEFINER` body.

  * **NOT DETERMINISTIC**

    A procedure is assumed nondeterministic, meaning it can return different results on each invocation, even when it's called with the same arguments.

  * **COMMENT procedure\_comment**

    A comment for the procedure. `procedure_comment` must be `STRING` literal. The default is `NULL`.

  * **DEFAULT COLLATION [default\_collation\_name](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-names.md#collation-name)**

    **Applies to:** ![check marked yes](https://docs.databricks.com/gcp/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/gcp/en/images/icons/check.png) Databricks Runtime 17.1 and above

    Sets the default collation of the procedure.
    The default collation of the procedure is used as the default collation for procedure parameters, `DEFAULT` expressions for parameters, `STRING` typed local variables declared in the procedure body, and `STRING` literals used in the procedure body.

    In Databricks Runtime 17.1 through Databricks Runtime 18.2, `default_collation_name` must be `UTF8_BINARY`.
    This clause is mandatory if the schema in which the procedure is created has a default collation other than `UTF8_BINARY`.

    **Applies to:** ![check marked yes](https://docs.databricks.com/gcp/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/gcp/en/images/icons/check.png) Databricks Runtime 18 LTS and above

    > [!NOTE]
    >
    > Databricks Runtime 18 is newer than Databricks Runtime 18.0, 18.1, and 18.2. Features that would previously have shipped as a later numbered version now ship as dated updates to Databricks Runtime 18 instead. For details, see [About unified release notes](https://docs.databricks.com/gcp/en/release-notes/runtime/18.md#unified-format).

    `default_collation_name` can be any supported [collation name](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-names.md#collation-name).

    If not specified, the default collation is derived from the schema in which the procedure is created.

  * **MODIFIES SQL DATA**

    A procedure is assumed to modify SQL data.

## Common error conditions

* [DUPLICATE\_CLAUSES](https://docs.databricks.com/gcp/en/error-messages/error-classes.md#duplicate_clauses)
* [DUPLICATE\_ROUTINE\_PARAMETER\_NAMES](https://docs.databricks.com/gcp/en/error-messages/error-classes.md#duplicate_routine_parameter_names)
* [INVALID\_DEFAULT\_VALUE](https://docs.databricks.com/gcp/en/error-messages/invalid-default-value-error-class.md)
* [INVALID\_SQL\_SYNTAX.CREATE\_ROUTINE\_WITH\_IF\_NOT\_EXISTS\_AND\_REPLACE](https://docs.databricks.com/gcp/en/error-messages/invalid-sql-syntax-error-class.md#create_routine_with_if_not_exists_and_replace)
* [MISSING\_CLAUSES\_FOR\_OPERATION](https://docs.databricks.com/gcp/en/error-messages/error-classes.md#missing_clauses_for_operation)
* [PROCEDURE\_CREATION\_EMPTY\_ROUTINE](https://docs.databricks.com/gcp/en/error-messages/error-classes.md#procedure_creation_empty_routine)
* [PROCEDURE\_CREATION\_PARAMETER\_OUT\_INOUT\_WITH\_DEFAULT](https://docs.databricks.com/gcp/en/error-messages/error-classes.md#procedure_creation_parameter_out_inout_with_default)
* [PROCEDURE\_NOT\_SUPPORTED](https://docs.databricks.com/gcp/en/error-messages/error-classes.md#procedure_not_supported)
* [PROCEDURE\_NOT\_SUPPORTED\_WITH\_HMS](https://docs.databricks.com/gcp/en/error-messages/error-classes.md#procedure_not_supported_with_hms)
* [ROUTINE\_ALREADY\_EXISTS](https://docs.databricks.com/gcp/en/error-messages/error-classes.md#routine_already_exists)
* [UNSUPPORTED\_PROCEDURE\_COLLATION](https://docs.databricks.com/gcp/en/error-messages/error-classes.md#unsupported_procedure_collation)

## Examples

```sql
-- Demonstrate INOUT and OUT parameter usage.
> CREATE OR REPLACE PROCEDURE add(x INT, y INT, OUT sum INT, INOUT total INT)
    LANGUAGE SQL
    SQL SECURITY INVOKER
    COMMENT 'Add two numbers'
    AS BEGIN
        SET sum = x + y;
        SET total = total + sum;
    END;
> DECLARE sum INT;
> DECLARE total INT DEFAULT 0;
> CALL add(1, 2, sum, total);
> SELECT sum, total;
 3 3
> CALL add(3, 4, sum, total);
 7 10

-- The last executed query is the result set of a procedure
> CREATE PROCEDURE greeting(IN mode STRING COMMENT 'informal or formal')
    LANGUAGE SQL
    SQL SECURITY INVOKER
    AS BEGIN
        SELECT 'Hello!';
        CASE mode WHEN 'informal' THEN SELECT 'Hi!';
                  WHEN 'formal' THEN SELECT 'Pleased to meet you.';
        END CASE;
    END;
> CALL greeting('informal');
  Hi!

> CALL greeting('formal');
  Pleased to meet you.

> CALL greeting('casual');
  Hello!

-- Use SQL SECURITY DEFINER so the procedure runs with the owner's privileges
-- and references its creation-time catalog and schema. The invoker only needs
-- EXECUTE on `audit_app.ops.log_event`; they do not need any privileges on the
-- underlying `audit_app.private.audit_log` table.
> USE CATALOG audit_app;
> USE SCHEMA ops;
> CREATE OR REPLACE PROCEDURE log_event(IN event STRING)
    LANGUAGE SQL
    SQL SECURITY DEFINER
    MODIFIES SQL DATA
    AS BEGIN
        INSERT INTO audit_app.private.audit_log
          VALUES (current_user(), current_catalog(), current_schema(), event);
    END;

-- Even when invoked from a different catalog/schema and by a different user,
-- the body still inserts into `audit_app.private.audit_log`, with
-- `current_catalog()` and `current_schema()` returning the values frozen at
-- creation time. `session_user()` is unaffected by `SQL SECURITY DEFINER`
-- and records the actual invoker -- which is what audit logs typically want.
> USE CATALOG sales;
> USE SCHEMA reports;
> CALL audit_app.ops.log_event('checkout_completed');
```

## Related articles

* [CALL](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-syntax-aux-call.md)
* [PATH](https://docs.databricks.com/gcp/en/sql/language-manual/parameters/path.md)
* [SET PATH](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-syntax-aux-conf-mgmt-set-path.md)
* [`current_path` function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/current_path.md)
* [DROP PROCEDURE](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-syntax-ddl-drop-procedure.md)
* [SHOW PROCEDURES](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-syntax-aux-show-procedures.md)
* [DESCRIBE PROCEDURE](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-syntax-aux-describe-procedure.md)
* [GRANT](https://docs.databricks.com/gcp/en/sql/language-manual/security-grant.md)
* [REVOKE](https://docs.databricks.com/gcp/en/sql/language-manual/security-revoke.md)
