Skip to main content
The CREATE PROCEDURE defines a .

Required privileges

  • To define a procedure, a user must have on the schema of the procedure.
  • To define a procedure with a , a user must have on the user-defined type.
  • To resolve a procedure, a user must have at least the on the schema of the procedure.
  • To , a user must have on the procedure.
  • At procedure definition and execution time, a user must have privileges on all the objects referenced in the procedure body. Privileges on referenced objects can be revoked and later procedure calls can fail due to lack of permission.
If you grant EXECUTE privilege as a default privilege at the database level, newly created procedures inherit that privilege from the database.

Synopsis

create_proc syntax diagram

Parameters

ParameterDescription
routine\_create\_nameThe name of the procedure.
routine\_paramA comma-separated list of procedure parameters, specifying the mode, name, and type.
routine\_body\_strThe body of the procedure. For allowed contents, see .

Examples

The following are examples of basic stored procedures. For a more detailed example of a stored procedure, see .

Create a stored procedure that uses a composite-type variable

Create a :
Create the procedure, declaring the comp variable you created:

Create a stored procedure that uses OUT and INOUT parameters

The following example uses a combination of OUT and INOUT parameters to modify a provided value and output the result. An OUT parameter returns a value, while an INOUT parameter passes an input value and returns a value.
When calling a procedure, you need to supply placeholder values for any OUT parameters. A NULL value is commonly used. When calling a procedure from another routine, you should declare variables that will store the results of the OUT parameters.

Create a stored procedure that calls a procedure

The following example defines a procedure that calls the double_triple example procedure. The triple_result variable is assigned the result of the OUT parameter, while the double_input variable both provides the input and stores the result of the INOUT parameter.
A procedure with OUT parameters can only be .

Create a stored procedure that uses conditional logic

The following example uses :

Create a stored procedure that uses a WHILE loop

The following example uses :

See also