Using EXIT and labels to exit blocks of statements

Lists: pgsql-novice
From: Danny Lo <lo(dot)dannyk(at)gmail(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Using EXIT and labels to exit blocks of statements
Date: 2010-03-02 21:39:42
Message-ID: c824170b1003021339u3d4a459dvca40ec945050a7f3@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

> Hi, My novice question is – I’d like to use EXIT statement to exit a
> block of statements. I copied an example from pg 809 of postgresql 8.4
> documentation but received
>
> Following error. Just want to know correct way to use label. Thanks very
> much.
>
>
>
> ERROR: label does not exist at or near "ablock"
>
> LINE 15: EXIT ablock; -- causes exit from the BEGIN block
>
> ^
>
>
>
> DROP FUNCTION IF EXISTS test_exit();
>
> CREATE OR REPLACE FUNCTION test_exit()
>
> RETURNS void AS
>
> $BODY$
>
>
>
> DECLARE
>
> x integer :=0;
>
> stocks bigint := 100100;
>
>
>
> <<ablock>>
>
> BEGIN
>
> -- some computations
>
> IF stocks > 100000 THEN
>
> EXIT ablock; -- causes exit from the BEGIN
> block
>
> END IF;
>
> -- computations here will be skipped when stocks > 100000
>
>
>
> END;
>
>
>
> $BODY$
>
>
>
> LANGUAGE 'plpgsql' VOLATILE
>
> COST 100;
>
> ALTER FUNCTION test_exit() OWNER TO postgres;
>
>
>
>


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Danny Lo <lo(dot)dannyk(at)gmail(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Using EXIT and labels to exit blocks of statements
Date: 2010-03-03 00:58:29
Message-ID: 27678.1267577909@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

Danny Lo <lo(dot)dannyk(at)gmail(dot)com> writes:
>> Following error. Just want to know correct way to use label. Thanks very
>> much.

You need to put the <<label>> before the DECLARE, not after --- see the
syntax examples here:
http://www.postgresql.org/docs/8.4/static/plpgsql-structure.html

I think it's a bug that it's not throwing an error for the label
placement as you have it.

regards, tom lane