Re: A question about trigger fucntion syntax

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: stan <stanb(at)panix(dot)com>
Cc: pgsql-general <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: A question about trigger fucntion syntax
Date: 2019-08-11 13:20:38
Message-ID: CAFj8pRCTKO2jin9RFccMKC9wui+=65vAPTSx-Vi2yXe2yDLY=g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi

> BTW, here is what I Ave tried.
>
> CREATE OR REPLACE FUNCTION check_permission()
> RETURNS trigger AS
> $BODY$
> BEGIN
> SELECT
> permit
> FROM
> permitted_work
> WHERE
> NEW.employee_key = OLD.employee_key
> AND
> NEW.work_type_key = OLD.work_type_key
>
> RETURN permit;
> END;
> $BODY$
> LANGUAGE PLPGSQL;
>
> and when I try to insert it I get a syntax error at the RETURN
>

there is more than one issue

1) trigger function should to returns record type (with same type like
table joined with trigger). Column permit is a boolean, so some is wrong.

2) the structure of your function is little bit strange. Probably you want
some like

CREATE OR REPLACE FUNCTION check_permission()
RETURNS trigger AS $$
DECLARE _permit boolean; -- variables should be declared;
BEGIN
SELECT permit INTO _permit -- result should be assigned to variable
FROM permitted_work
...;
IF NOT permit THEN
RAISE EXCEPTION 'some error message';
END IF;

RETURN NEW;
END;
$$ LANGUAGE plpgsql;

Regards

Pavel

>
> --
> "They that would give up essential liberty for temporary safety deserve
> neither liberty nor safety."
> -- Benjamin Franklin
>
>
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Rob Sargent 2019-08-11 13:38:51 Re: Quoting style (was: Bulk Inserts)
Previous Message stan 2019-08-11 13:06:19 Re: A question about trigger fucntion syntax