DunDmy
DunDmy

Reputation: 275

Function Doesn't Work: ERROR: syntax error at or near "DELETE"

I am trying to create a function inside Postgres. For some reason my query works perfectly fine when I run it from inside the Query tool. Once I added the query to my function, I get the error

ERROR: syntax error at or near "DELETE"

Not sure what is the problem.

CREATE FUNCTION public.remove_email_header()
    RETURNS pg_trigger
    LANGUAGE 'plpgsql'

AS $BODY$
DELETE FROM public.spam_dictionary WHERE words LIKE '%FROM%';
$BODY$;

ALTER FUNCTION public.remove_email_header()
    OWNER TO postgres;

Upvotes: 0

Views: 481

Answers (1)

Kaushik Nayak
Kaushik Nayak

Reputation: 31648

Two Things to note.

A plpgsql Postgres function uses BEGIN END

The plpgsql should not be quoted.

CREATE FUNCTION remove_email_header()
    RETURNS trigger
    LANGUAGE plpgsql

AS $BODY$
BEGIN
DELETE FROM public.spam_dictionary WHERE words LIKE '%FROM%';
END 
$BODY$;

Upvotes: 1

Related Questions