Re: How to raise error from PostgreSql SQL statement if some condition is met

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Andrus <kobruleht2(at)hot(dot)ee>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: How to raise error from PostgreSql SQL statement if some condition is met
Date: 2012-08-11 20:46:33
Message-ID: CAFj8pRC3aOx7+PTXAdZtx=j897sAF1UHUVk_zWTcFOZdysVYoA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello

You can execute only SQL statements - RAISE is plpgsql statement, not
SQL statement, so you cannot execute it.

why you don't use just

CREATE OR REPLACE FUNCTION raise_exception(text)
RETURNS void AS $$
BEGIN
RAISE EXCEPTION '%', $1;
END;
$$ LANGUAGE plpgsql;

SELECT raise_exception('bubu');

Regards

Pavel Stehule

2012/8/11 Andrus <kobruleht2(at)hot(dot)ee>:
> I’m looking for a way to raise error from sql select if some condition is
> met.
> Tried code below to got error shown in comment.
> How to fix ?
>
> Andrus
>
> CREATE OR REPLACE FUNCTION "exec"(text)
> RETURNS text AS
> $BODY$
> BEGIN
> EXECUTE $1;
> RETURN $1;
> END;
> $BODY$
> LANGUAGE plpgsql VOLATILE;
>
> -- ERROR: syntax error at or near "raise"
> -- LINE 1: raise 'test'
>
> select exec('raise ''test'' ') where true -- in real application true is
> replaced by some condition

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Craig Ringer 2012-08-12 03:26:59 Re: How to raise error from PostgreSql SQL statement if some condition is met
Previous Message Andrus 2012-08-11 19:07:25 How to raise error from PostgreSql SQL statement if some condition is met