I believe the logic of what you are trying to accomplish is covered on this
page[
^]. Aside from that:
1. You should
DECLARE
variables inside the
BEGIN
and
END
statements.
2. Add closing parenthesis to the
RETURNS
table definition.
3. Don't use
@
in the column names for the
RETURNS
table definition.
4. Also, consider using variable, table, function, column names that are not as similar as the ones here to avoid confusion and make the function more readable.