stringpostgresql-9.2

String concatenation within an exception


In my trigger procedures I use RAISE EXCEPTION for messages. I have no problem with simple messages, but if I want to give the user some more complex feedback, I face a problem: the concatenation operator doesn't work within RAISE EXCEPTION statement.

First, I tried this:

CREATE OR REPLACE FUNCTION hlidej_datum_kon() RETURNS trigger AS $$
DECLARE
  od date;
BEGIN
  SELECT a.datum_od FROM akce AS a WHERE a.kod_akce = (
    SELECT b.kod_akce FROM sj AS b WHERE b.kod_sj = NEW.kod_sj
  ) INTO od;
  IF NEW.datum < od THEN
    RAISE EXCEPTION 'Kontext nemohl být odkryt před začátkem akce ('||TO_CHAR(od)||')!'
    ROLLBACK;  
  END IF;
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

Didn't work. So I tried to put the whole text to a text variable, but I didn't find how to put the variable's contents to the exception statement so that it would be printed as a message.

My question is: how to print a message containing variables in a PostgreSQL trigger function?

Just for sake of completeness, here is my trigger:

CREATE TRIGGER hlidej_datum_kon
AFTER INSERT OR UPDATE ON kontext
FOR EACH ROW
  EXECUTE PROCEDURE hlidej_datum_kon();
END;

Solution

  • There are two bugs