I am unable to fix a syntax error that I keep getting when trying to create a function in Snowflake. This works fine when I create a procedure, but whenever I try to create a function with a variable declaration, I keep getting the error below. I want this to be a function since it only returns a single, scalar value.
Syntax error: unexpected 'l_emp_excess_balance'.t
Here is the code:
CREATE OR REPLACE FUNCTION MDT_PRESDEV_DB_ADMIN.PERS.begin_bal_emp_excess_comp(psid VARCHAR)
RETURNS NUMBER
AS
$$
DECLARE
l_emp_excess_balance NUMBER;
BEGIN
SELECT COALESCE(v.peec_excess_leave_hrs, 0)
INTO l_emp_excess_balance
FROM MDT_PRESDEV_DB_ADMIN.pers_excess_xmpt_comp_leave v
WHERE v.peec_ps_id = psid
AND v.peec_system = 'PERS'
AND v.peec_year = (
SELECT MAX(m.peec_year)
FROM MDT_PRESDEV_DB_ADMIN.pers_excess_xmpt_comp_leave m
WHERE m.peec_system = 'PERS'
);
RETURN COALESCE(l_emp_excess_balance, 0);
END;
$$;
I tried creating a procedure instead of a function. That worked, but I would much rather this just be a function.