Skip to content
Developer Edition runs on one host and does not require a product license file. Need multi-host deployment or performance testing at scale? Explore Enterprise Edition

CREATE PROCEDURE statement

The CREATE PROCEDURE statement creates a user-defined procedure (UDP) in the used schema. All UDPs must be named procedures.

The newly created UDP is owned by the user executing this statement. The owner of the UDP can call, alter, or drop the UDP after it is created. The ownership of the UDP can be transferred to another user by using ALTER OWNER.

Note

An operation involving a non-default schema or an object in it requires access to the schema, either implicitly or through an explicit USAGE grant, as well as the privilege required for the operation. The schema access check and the privilege check for the requested operation are independent; satisfying one does not satisfy the other. Schema access control does not apply to temporary graphs or built-in procedures and functions. For more information, see Schema access control.

Prerequisites

To create a UDP, you must be granted to perform the action CREATE PROCEDURE on the used schema. For more information, see Roles and privileges.

Syntax

create_procedure_statement
CREATE PROCEDURE IF NOT EXISTS OR REPLACE PROCEDURE procedure_name ( parameter_declaration , ) returns_clause COMMENT comment_string AS { procedure_body }
parameter_declaration
parameter_name parameter_type DEFAULT default_value
returns_clause
RETURNS ( ) RETURNS return_name return_type RETURNS ( return_name return_type , return_name return_type )

create_procedure_statement ::=
    'CREATE' (('PROCEDURE' ('IF NOT EXISTS')?) | ('OR REPLACE' 'PROCEDURE')) procedure_name '(' ( parameter_declaration ( ',' parameter_declaration )* )? ')' returns_clause? ('COMMENT' comment_string)? 'AS'? '{' procedure_body '}' 

parameter_declaration  ::=
    parameter_name parameter_type ('DEFAULT' default_value )?   

returns_clause ::=
      'RETURNS' '(' ')'
    | 'RETURNS' return_name return_type
    | 'RETURNS' '(' return_name return_type (',' return_name return_type )+ ')'

Parameters

IF NOT EXISTS

This clause checks if a UDP with the same name already exists. If it does already exist and cannot be overloaded, the UDP is not created and no exception is thrown.

If you do not specify this clause, an exception is thrown if a UDP with the same name already exists and the UDP cannot be overloaded.

OR REPLACE

This clause replaces the definition of an existing UDP. If the UDP does not exist, a new UDP is created.

Note that the name, input parameters, and output parameters of the existing UDP must NOT be changed when using this clause.

procedure_name

The name of the UDP to be created.

procedure_name must be an identifier. For more information about identifiers, see Rules for naming identifiers.

Note

While it is mandatory to specify unique names for UDPs when you execute CREATE PROCEDURE, it is still possible to have UDPs with duplicate names in the same schema if you load procedure plugins to add UDPs to NebulaGraph Database Developer Edition. In case a UDP with duplicate names is called, the one defined by the plugin takes precedence.

Overloading is supported. You can create multiple UDPs with the same name but different input parameters as long as no conflict exists when calling the UDPs. For more information, see Overload UDPs.

parameter_name

The name of the input parameter accepted by the UDP. The name must be unique within the UDP.

parameter_name must be an identifier. For more information about identifiers, see Rules for naming identifiers.

parameter_type

The input parameter type, which can be an ordinary value type, graph element reference type, or table type.

  • For more information about data types, see Data types.
  • To specify a graph element reference type, use parameter_name NODE or parameter_name EDGE to indicate that the parameter accepts a node reference value or an edge reference value, respectively.
  • To specify a table type, use the syntax parameter_name TABLE to indicate that the parameter accepts a table variable.

default_value

The default value of the input parameter. The default value must be a constant value. For more information, see Constant value expression.

Note that parameters with default values must be specified from right to left. For example, test(a STRING, b STRING DEFAULT 'Hello') is valid, while test(a STRING DEFAULT 'Hello', b STRING) is invalid.

return_name

The name of the output parameter returned by the UDP. The name must be unique within the UDP. The number of output parameters must be the same as the number of fields returned by procedure_body.

return_name must be an identifier. For more information about identifiers, see Rules for naming identifiers.

return_type

The data type of the output parameter returned by the UDP. For more information, see Data types.

comment_string

The description of the UDP. The description must be a string literal enclosed in double quotes (") or single quotes ('). For more information, see String literals.

procedure_body

The processing logic of the UDP. For more information, see Procedure body.

Examples

The following examples are based on the example dataset.

Create a UDP without input parameters

CREATE PROCEDURE test1() RETURNS ret INT AS { RETURN 1 }

Create a UDP with an input parameter

'''
// Create a UDP with an input parameter of INT type
CREATE OR REPLACE PROCEDURE set_proc_param_test1 (a INT) RETURNS r INT  {
  SET a = a + 1
  RETURN a
}
'''

'''
// Create a UDP with an input parameter of node type 
CREATE OR REPLACE PROCEDURE set_proc_param_node (a NODE) RETURNS r INT  { RETURN 1 }
'''

'''
// Create a UDP with an input parameter of edge type
CREATE OR REPLACE PROCEDURE set_proc_param_edge (a EDGE) RETURNS r INT  { RETURN 1 }
'''

'''
// Create a UDP with an input parameter of table type
CREATE OR REPLACE PROCEDURE set_proc_param_table (a TABLE) RETURNS r INT  { RETURN size(a) AS r }
'''

Create a UDP with input parameters

CREATE PROCEDURE test2(a INT, b STRING DEFAULT 'test2') RETURNS ret INT AS { RETURN a }

Create a UDP without return parameters

'''
// Method one
CREATE PROCEDURE test3(a INT, b STRING) RETURNS () AS { LET y = 2 FINISH }
'''

'''
// Method two
CREATE PROCEDURE test3(a INT, b STRING) AS { LET y = 2 FINISH }
'''

Create a UDP with multiple return parameters

CREATE PROCEDURE test4(a INT, b STRING) RETURNS (x INT, y STRING) AS { RETURN a, b }

Replace an existing UDP

CREATE OR REPLACE PROCEDURE test1() RETURNS ret INT COMMENT 'test function 1' AS { RETURN 2 }

Overload UDPs

CREATE PROCEDURE test(a INT) RETURNS ret INT AS { RETURN a }
CREATE PROCEDURE test(a INT, b STRING) AS { FINISH }
CREATE PROCEDURE test(a INT, b INT) RETURNS ret INT AS { RETURN b }
CREATE PROCEDURE test(a INT16, b INT32) RETURNS ret INT AS { RETURN a+b }

The preceding statements create four UDPs with the same name test but different input parameters.

Create a UDP that updates a graph

CREATE OR REPLACE PROCEDURE update_name (n STRING) RETURNS () { USE movie MATCH (v@Actor {id: 1608}) SET v.name = n }

The preceding statement creates a UDP named update_name that updates the name of the node with the ID 1608 to the value of the input parameter n.

More information