user195257
user195257

Reputation: 3316

ERROR: SQL Functions cannot return type trigger in PostgreSQL

I'm using PostgreSQL with pgAdmin and I can't get a trigger function to work. However, as far as I am aware, you can return type trigger in PostgreSQL?

CREATE OR REPLACE FUNCTION validate_Cat() 
  RETURNS TRIGGER AS 
$BODY$
BEGIN
  -- CODE here
END;
$BODY$
LANGUAGE SQL;
    
CREATE TRIGGER validate_Cat
AFTER INSERT OR UPDATE ON Category
FOR EACH ROW execute procedure validate_Cat();

Upvotes: 12

Views: 5476

Answers (2)

I got the same error below:

ERROR: SQL functions cannot return type trigger

When I tried to create the trigger function with LANGUAGE SQL as shown below:

CREATE FUNCTION my_func() RETURNS trigger
LANGUAGE SQL -- Here
AS $$
$$;

But, I could create the trigger function with LANGUAGE plpgsql without error as shown below:

CREATE FUNCTION my_func() RETURNS trigger
LANGUAGE plpgsql -- Here
AS $$
BEGIN
END;
$$;

In addition, I got the error below:

ERROR: SQL functions cannot return type event_trigger

When I tried to create the event trigger function with LANGUAGE SQL as shown below:

CREATE FUNCTION my_func()
RETURNS event_trigger
LANGUAGE SQL -- Here
AS $$
$$;

But, I could create the event trigger function with LANGUAGE plpgsql without error as shown below:

CREATE FUNCTION my_func()
RETURNS event_trigger
LANGUAGE plpgsql -- Here
AS $$
BEGIN
END;
$$;

Upvotes: 1

user195257
user195257

Reputation: 3316

SOLVED, had to change language to PLPGSQL

Upvotes: 27

Related Questions