> ## Documentation Index
> Fetch the complete documentation index at: https://docs.kinetica.com/llms.txt
> Use this file to discover all available pages before exploring further.

# CREATE PROCEDURE

Creates a new [SQL procedure](https://docs.kinetica.com/content/sql/procedure/create-procedure.md#sql-procedures-create).

```sql title="CREATE PROCEDURE Syntax" theme={null}
CREATE [OR REPLACE] PROCEDURE [<schema name>.]<procedure name> [()]
[LANGUAGE SQL]
BEGIN
    <sql statements>
END
[
    EXECUTE FOR EVERY <number> <SECOND[S] | MINUTE[S] | HOUR[S] | DAY[S]>
        [STARTING AT '<YYYY-MM-DD [HH:MM[:SS]]>']
        [STOP AFTER < '<YYYY-MM-DD [HH:MM[:SS]]>' | <number> <SECOND[S] | MINUTE[S] | HOUR[S] | DAY[S]> >]
]
[WITH OPTIONS (<stored procedure property name> = '<stored procedure property value>'[,...])]
```

When any of the source *tables* referenced by a *SQL procedure* is altered or
dropped, the *SQL procedure* will also be dropped.

## Parameters

> [accordion] \<schema name>
>
> Name of the schema in which this *SQL procedure* will be created.

> [accordion] \<procedure name>
>
> Name to give to the created *SQL procedure*; must adhere to the supported
> [naming criteria](https://docs.kinetica.com/content/sql/naming.md#sql-naming-criteria) for tables, and cannot be named the
> same as any existing table or view.

> [accordion] \<sql statements>
>
> Semicolon-separated list of
> [supported SQL statements](https://docs.kinetica.com/content/sql/procedure/supported-statements.md#sql-procedures-statements).  If the final statement
> in the *SQL procedure* is an output-generating statement
> *(SQL query, SHOW command, etc.)*, the output of that statement alone will be returned
> to the user; all other statements that generate output will be ignored.

> [accordion] OR REPLACE
>
> Drop any existing *SQL procedure* with the same name before creating this one.

> [accordion] LANGUAGE
>
> Optional language specification.  Only `SQL` is supported at this time.

> [accordion] EXECUTE FOR EVERY
>
> Allows specification of an interval in seconds, minutes, hours, or days, at which the
> *SQL procedure* should be executed.  Fractional values for `<number>` are accepted.
>
> By default, the procedure will be run immediately and every specified interval after
> that; this can be modified with the following options:
>
> * `STARTING AT`:  specify a date or timestamp at which the first execution should
>   occur
> * `STOP AFTER`:  specify a date, timestamp, or time interval after which executions
>   should end

> [accordion] WITH OPTIONS
>
> Optional indicator that a comma-delimited list of option/value assignments will follow.
> The following options are available:
>
> <table>
>   <thead>
>     <tr>
>       <th>Option</th>
>       <th>Description</th>
>     </tr>
>   </thead>
>
>   <tbody>
>     <tr>
>       <td>`EXECUTE AS`</td>
>       <td>Executes the *SQL procedure* as the given user with that user's privileges. \[!NOTE] If this user doesn't exist at the time of execution, the *SQL procedure* will be executed as the creating user, and and, failing that, the system administration user.</td>
>     </tr>
>   </tbody>
> </table>

## Examples

To create a `sqlp` *SQL procedure*:

```sql CREATE PROCEDURE Example theme={null}
CREATE PROCEDURE example.sqlp
BEGIN
	CREATE OR REPLACE TABLE example.sqlp_table_tmp AS
	SELECT *
	FROM example.sqlp_table_ref;

	SELECT *
	FROM example.sqlp_table_tmp;
END
```

To create a `sqlp_weekly` *SQL procedure* that executes once per week:

```sql CREATE PROCEDURE (Scheduled) Example theme={null}
CREATE PROCEDURE example.sqlp_weekly
BEGIN
	CREATE OR REPLACE TABLE example.sqlp_table_tmp AS
	SELECT *
	FROM example.sqlp_table_ref;

	SELECT *
	FROM example.sqlp_table_tmp;
END
EXECUTE FOR EVERY 7 DAYS
STARTING AT '2019-01-01 00:00:00'
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.