0

I wonder how to use trigger in PostgreSQL with RAISE SQLSTATE dynamic instruction?

IF i>0 THEN
    RAISE SQLSTATE '23505' USING MESSAGE = 'la planète est déjà occupée (planet_non_free)=(%, %, %)', NEW.galaxie, NEW.systeme_solaire, NEW.position;
END IF;

It doesn't work.

4
  • 1
    "doesn't work" is not a problem description. Commented Dec 25, 2012 at 19:55
  • Ok, I just found the solution...just surrounded MESSAGE value by parenthesis like this : MESSAGE = ('la planète..., NEW.position); Commented Dec 25, 2012 at 19:58
  • You should write it up as an answer and accept it. :-) Commented Dec 25, 2012 at 19:59
  • What PostgreSQL version is this supposed to be? Also, "It doesn't work" is not a PostgreSQL error message known to me. It should go without saying to include the actual error message. If your system runs with a french locale, just run SET lc_messages='C'; to switch to English for the current session. Commented Dec 26, 2012 at 4:03

2 Answers 2

1

I suggest to use:

IF i > 0 THEN
    RAISE SQLSTATE '23505'
    USING MESSAGE = format('la planète est déjà occupée (planet_non_free)=(%s, %s, %s)', NEW.galaxie, NEW.systeme_solaire, NEW.position);
END IF;

Then your error message reads:

la planète est déjà occupée (planet_non_free)=(<g>, <s>, <p>)

.. instead of:

("la planète est déjà occupée (planet_non_free)=(%, %, %)",<g>, <s>, <p>)
Sign up to request clarification or add additional context in comments.

Comments

0

Ok, I've found the solution. I have only to surround MESSAGE value with parenthesis :

So :

IF i>0 THEN
   RAISE SQLSTATE '23505' USING MESSAGE = 'la planète est déjà occupée (planet_non_free)=(%, %, %)', NEW.galaxie, NEW.systeme_solaire, NEW.position;
END IF;

becomes :

IF i>0 THEN
   RAISE SQLSTATE '23505' USING MESSAGE = ('la planète est déjà occupée (planet_non_free)=(%, %, %)', NEW.galaxie, NEW.systeme_solaire, NEW.position);
END IF;

Simple but I did not saw explicit example with dynamic MESSAGE on the web.

Hope this helps

EDIT :

Ok sorry, the right syntax is this one :

IF i>0 THEN
    RAISE SQLSTATE '23505' USING MESSAGE = 'la planète est déjà occupée (planet_non_free)=(' || NEW.galaxie || ',' || NEW.systeme_solaire || ',' || NEW.position || ')';
END IF;

It seems that we can not use % with USING MESSAGE statement.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.