- on-demand - called directly by a user
- scheduled execution - configured, upon creation, to execute at a user-specified interval
Even if a SQL procedure is configured for scheduled execution, it
can still be executed directly by a user in on-demand fashion.
If any of the database objects referenced in the SQL procedure are
dropped or modified, the SQL procedure will be dropped as well. This does
not include any objects created by the SQL procedure that are later
referenced by it.
Supported Statements
The following statement types are allowed within a SQL procedure:SELECTINSERT(inserting from data file not supported)UPDATEDELETECHECK PERMISSION<CREATE | DROP | SHOW CREATE> SCHEMA<CREATE | TRUNCATE | DROP | SHOW CREATE> TABLECREATE TABLE ... AS<CREATE [MATERIALIZED] | REFRESH | DROP | SHOW CREATE> VIEWDESCRIBEEXECUTE FUNCTIONEXECUTE PROCEDUREREFRESH MATERIALIZED VIEWSHOW PROCEDURESHOW SECURITY [FOR <USER | ROLE>]SHOW RESOURCE GROUP
Any unqualified table/view references made within the SQL procedure
will be assumed to exist in the creator’s default schema at the time of
creation. Those references will be set upon creation and never re-evaluated
afterwards, regardless of the execution type of the SQL procedure, who
executes it, or what
EXECUTE AS option is specified.CREATE PROCEDURE
Creates a new SQL procedure.CREATE PROCEDURE Syntax
Parameters
<schema name>
<schema name>
Name of the schema in which this SQL procedure will be created.
<procedure name>
<procedure name>
Name to give to the created SQL procedure; must adhere to the supported
naming criteria for tables, and cannot be named the
same as any existing table or view.
<sql statements>
<sql statements>
Semicolon-separated list of
supported SQL 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.
OR REPLACE
OR REPLACE
Drop any existing SQL procedure with the same name before creating this one.
LANGUAGE
LANGUAGE
Optional language specification. Only
SQL is supported at this time.EXECUTE FOR EVERY
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 occurSTOP AFTER: specify a date, timestamp, or time interval after which executions should end
WITH OPTIONS
WITH OPTIONS
Optional indicator that a comma-delimited list of option/value assignments will follow.
The following options are available:
| Option | Description |
|---|---|
EXECUTE AS | Executes the SQL procedure as the given user with that user’s privileges. 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. |
Examples
To create asqlp SQL procedure:
CREATE PROCEDURE Example
sqlp_weekly SQL procedure that executes once per week:
CREATE PROCEDURE (Scheduled) Example
ALTER PROCEDURE
Alters the configuration of an existing SQL procedure. The following facets of a SQL procedure can be altered:Set Execution User
A SQL procedure can have its execution user modified.ALTER PROCEDURE SET EXECUTE AS Syntax
Examples
To set the execution user of thesqlp_ea SQL procedure to spuser:
ALTER PROCEDURE SET EXECUTE AS Example
Set Execute Mode
The refresh mode of a SQL procedure can be modified.ALTER PROCEDURE SET EXECUTE MODE Syntax
Parameters
EXECUTE FOR EVERY
EXECUTE FOR EVERY
Specify an interval in seconds, minutes, hours, or days at which the SQL procedure should be
executed.By default, the first execution interval will be one interval’s worth of time from the point at
which the SQL procedure alteration was requested.
STARTING AT
STARTING AT
Specify a date or timestamp at which refresh cycles should begin.
STOP AFTER
STOP AFTER
Specify a date, timestamp, or time interval after which refresh cycles should end.
Examples
To alter thesqlp_em SQL procedure to execute every 6 hours:
ALTER PROCEDURE Set Execution Mode Example
ALTER PROCEDURE Set Execution Start/Stop Example
EXECUTE PROCEDURE
SQL procedures can be executed on-demand.EXECUTE PROCEDURE Syntax
Examples
To execute thesqlp SQL procedure:
EXECUTE PROCEDURE Example
DROP PROCEDURE
Removes an existing SQL procedure.DROP PROCEDURE Syntax
Parameters
IF EXISTS
IF EXISTS
Optional error-suppression clause; if specified, no error will be returned if the given
procedure does not exist.
<schema name>
<schema name>
Name of the schema containing the procedure to remove.
<procedure name>
<procedure name>
Name of the procedure to remove.
Examples
To drop thesqlp SQL procedure:
DROP PROCEDURE Example
SHOW PROCEDURE
Shows the content of one or all existing SQL procedures.SHOW PROCEDURE Syntax
Parameters
<schema name>
<schema name>
Name of the schema containing the procedure to show.
<procedure name>
<procedure name>
Name of the procedure whose content will be output; use
* instead of
schema/procedure name to output the DDL of all procedures.Examples
To show the contents of thesqlp_weekly SQL procedure:
SHOW PROCEDURE Example
SHOW PROCEDURE (All SQL Procedures) Example
Security
Permissions for managing procedures follow those for creating tables; e.g., if a user has the ability to create a table in a given schema, that user will also be able to create & drop procedures there. Executing a procedure requires either the implicit execute permission that is granted to the creator of a procedure, or explicit execute permission, which can be granted to or revoked from any user or role, irrespective of whether the target user or role has the appropriate access to the database objects referenced within the SQL procedure. When procedures are executed on-demand, they are run, by default, with invoker rights (caller permissions); while those run automatically by scheduled execution are executed with definer rights (creator permissions). These defaults can be overridden by creating the stored procedure with theEXECUTE AS option, which allows a user to be designated for
permissions-checking when running the procedure. The EXECUTE AS user is
automatically given EXECUTE permission on the corresponding SQL procedure.
For scheduled execution runs, if the EXECUTE AS user loses EXECUTE
permission on the procedure or doesn’t have permissions to run the commands
within the procedure, the execution will fail accordingly. If the user doesn’t
exist, the procedure will be executed with definer rights; and if the creator
no longer exists, it will be executed as the system administration user.
For on-demand runs, if the EXECUTE AS user doesn’t have permissions to run
the commands within the procedure, the execution will fail accordingly. If the
user doesn’t exist, the procedure will be executed with invoker rights.
Execute permission on a procedure also allows the grantee to see the contents of
the procedure.