CREATE PROCEDURE
Defines a stored procedure that executes SQL operations and returns a result.
Syntax
CREATE PROCEDURE <procedure_name>(<parameter_name> <data_type>, ...)
RETURNS <return_data_type> [NOT NULL]
LANGUAGE <language>
[ COMMENT '<comment>' ]
AS $$
BEGIN
<procedure_body>
RETURN <return_value>; -- Use to return a single value
-- OR
RETURN TABLE(<select_query>); -- Use to return a table
END;
$$;
Access control requirements
To create a procedure, the user performing the operation or the current_role must have the CREATE PROCEDURE privilege.
Examples
This example defines a stored procedure that converts weight from kilograms (kg) to pounds (lb):
CREATE PROCEDURE convert_kg_to_lb(kg DECIMAL(4, 2))
RETURNS DECIMAL(10, 2)
LANGUAGE SQL
COMMENT = 'Converts kilograms to pounds'
AS $$
BEGIN
RETURN kg * 2.20462;
END;
$$;
You can also define a stored procedure that works with loops, conditions, and dynamic variables.
CREATE OR REPLACE PROCEDURE loop_test()
RETURNS INT
LANGUAGE SQL
COMMENT = 'loop test'
AS $$
BEGIN
LET x RESULTSET := select number n from numbers(10);
LET sum := 0;
FOR x IN x DO
FOR batch in 0 TO x.n DO
IF batch % 2 = 0 THEN
sum := sum + batch;
ELSE
sum := sum - batch;
END IF;
END FOR;
END FOR;
RETURN sum;
END;
$$;
-- Grant ACCESS PROCEDURE Privilege TO role test
GRANT ACCESS PROCEDURE ON PROCEDURE loop_test() to role test;
CALL PROCEDURE loop_test();
┌─Result─┐
│ -5 │
└────────┘