Firebird 3 trigger syntax

50 views
Skip to first unread message

Karol Bieniaszewski

unread,
Apr 25, 2024, 2:37:45 AM4/25/24
to firebird...@googlegroups.com

Hi

 

I use Firebird 3 and have problem with trigger

Inside it i have:

 

            IF (VAR_P_ID IS NULL) THEN

                BEGIN

                    VAR_WAS_PUNKT = 'F';

                   

                    VAR_P_ID = GEN_ID(GEN_PUNKT__ID, 1);

                    INSERT INTO PUNKT (ID, WOJ_ID, POW_ID, GMI_ID, MIEJSCOWOSC, POCZTA, KOD_POCZTOWY,

                        UL_SKROT, ULICA, NR_DOM, KRAJ, DT_INS, KIEDY, ZRODLO_INS, ZRODLO_MOD)

                    VALUES (

                        :VAR_P_ID,

                        :NEW.WOJ_ID,

                        :NEW.POW_ID,

                        :NEW.GMI_ID,

                        REMOVE_DOUBLE_CHAR(TRIM(:NEW.MIEJSCE_MIEJSCOW)),

                         REMOVE_DOUBLE_CHAR(TRIM(:NEW.MIEJSCE_POCZTA)),

                        :NEW.MIEJSCE_KOD_POCZT,

                        :NEW.MIEJSCE_UL_SKROT,

                        REMOVE_DOUBLE_CHAR(TRIM(:NEW.MIEJSCE_ULICA)),

                        REMOVE_DOUBLE_CHAR(TRIM(:NEW.MIEJSCE_NR_DOM)), 

                        COALESCE(NEW.MIEJSCE_KRAJ, 'PL'),

                        'now',

                        'now',

                        'k',

                        'k'

                    );

                    WHEN SQLCODE -803 DO

                        BEGIN

                            VAR_P_ID = NULL;

                        END

                       

                    NEW.PUNKT_ID = VAR_P_ID;

                END

 

I got an error:

SQL Message : -104

Invalid token

 

Engine Code    : 335544569

Engine Message :

Dynamic SQL Error

SQL error code = -104

Token unknown - line 345, column 6

NEW

 

When i remove

 

                    WHEN SQLCODE -803 DO

                        BEGIN

                            VAR_P_ID = NULL;

                        END

 

All is ok. How to fix syntax?

 

Regards,

Karol Bieniaszewski

 

Svein Erling Tysvær

unread,
Apr 25, 2024, 2:51:03 AM4/25/24
to firebird...@googlegroups.com
I don't think you're supposed to use : before NEW, Karol. I have no idea why it seems to work for you without WHEN

HTH,
Set

--
You received this message because you are subscribed to the Google Groups "firebird-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebird-suppo...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/firebird-support/4VQ5kn1QzWz20db%40smtp.poczta.onet.pl.

Karol Bieniaszewski

unread,
Apr 25, 2024, 3:35:34 AM4/25/24
to firebird...@googlegroups.com

Interesting that when i surround it with BEGIN END all is ok, strange, but fixed the problem..

 

            IF (VAR_P_ID IS NULL) THEN

                BEGIN

                    VAR_WAS_PUNKT = 'F';

                   

                    VAR_P_ID = GEN_ID(GEN_PUNKT__ID, 1);

                    BEGIN

Regards,

Karol Bieniaszewski

Pavel Cisar

unread,
Apr 25, 2024, 4:25:28 AM4/25/24
to firebird...@googlegroups.com
Hi Karol,

Dne 25. 04. 24 v 9:35 'Karol Bieniaszewski' via firebird-support napsal(a):
> Interesting that when i surround it with BEGIN END all is ok, strange,
> but fixed the problem..

You already discovered that, good. The reason is simple. WHEN clause
MUST be at the end of code block (begin ... end). It could be followed
only by another WHEN clause. But you put further code at the same block
level after WHEN clause, hence the parser did not recognized it as
valid. Sure, the error message was misleading, and not pointing to real
cause.

The easiest way to understand it is looking at it like TRY .. EXCEPT in
other languages. The the BEGIN is like TRY, and the WHEN is like EXCEPT,
i.e.

BEGIN -- TRY
... -- guarded code
WHEN ... DO -- EXCEPT
END -- end of guarded block

There is nothing like FINALLY or ELSE parts in PSQL.

See
https://firebirdsql.org/file/documentation/chunk/en/refdocs/fblangref30/fblangref30-psql-handleexceptions.html


best regards
Pavel Cisar
IBPhoenix

Vlad Khorsun

unread,
Apr 25, 2024, 4:25:42 AM4/25/24
to firebird-support
  If I understand you correctly, you have something like

begin
...
when ...
  begin
  end
-- some statements here
end

  This is wrong, and there could be no statements after WHEN block (i.e. between WHEN and END)

| The WHEN …​ DO block must be located at the very end of a block of statements, before the block’s END statement.


Regards,
Vlad

Karol Bieniaszewski

unread,
Apr 25, 2024, 11:51:09 AM4/25/24
to firebird...@googlegroups.com

Thank you Vlad

 

Then the syntax provided is a little misleading. Ok, description below described it clearly and it is ok, but:

 

Currently it is:

 

WHEN {<error> [, <error> ...] | ANY}

      DO <compound_statement>

      

      <error> ::=

        { EXCEPTION exception_name

        | SQLCODE number

        | GDSCODE errcode

        | SQLSTATE sqlstate_code }

 

 

But i suppose it should be, as the description say it explicitly thet after should be the END

 

BEGIN /* any BEGIN even trigger body BEGIN */

STATEMENT

WHEN {<error> [, <error> ...] | ANY}

      DO <compound_statement>

      

      <error> ::=

        { EXCEPTION exception_name

        | SQLCODE number

        | GDSCODE errcode

        | SQLSTATE sqlstate_code }

END /* any END even trigger body END */

 

Regards,

Karol Bieniaszewski

 

Od: Vlad Khorsun
Wysłano: czwartek, 25 kwietnia 2024 10:25
Do: firebird-support
Temat: Re: [firebird-support] Firebird 3 trigger syntax

 

  If I understand you correctly, you have something like

--

You received this message because you are subscribed to the Google Groups "firebird-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebird-suppo...@googlegroups.com.

Karol Bieniaszewski

unread,
Apr 25, 2024, 11:51:43 AM4/25/24
to firebird...@googlegroups.com

Good explanation, thank you 😊

 

Regards,

Karol Bieniaszewski

--

You received this message because you are subscribed to the Google Groups "firebird-support" group.

To unsubscribe from this group and stop receiving emails from it, send an email to firebird-suppo...@googlegroups.com.

Mark Rotteveel

unread,
Apr 25, 2024, 12:28:10 PM4/25/24
to firebird...@googlegroups.com
On 25/04/2024 17:51, 'Karol Bieniaszewski' via firebird-support wrote:
> Then the syntax provided is a little misleading. Ok, description below
> described it clearly and it is ok, but:

The problem is that that section only covers the WHEN DO itself, it
doesn't document the wider syntax. That said, the current documentation
is not ideal in this regard. I'll see if I can improve it.

> Currently it is:
>
> WHEN {<error> [, <error> ...] | ANY}
>       DO <compound_statement>
>
>       <error> ::=
>         { EXCEPTION exception_name
>         | SQLCODE number
>         | GDSCODE errcode
>         | SQLSTATE sqlstate_code }
>
> But i suppose it should be, as the description say it explicitly thet
> after should be the END
>
> BEGIN /* any BEGIN even trigger body BEGIN */
>
> STATEMENT
>
> WHEN {<error> [, <error> ...] | ANY}
>       DO <compound_statement>
>
>       <error> ::=
>         { EXCEPTION exception_name
>         | SQLCODE number
>         | GDSCODE errcode
>         | SQLSTATE sqlstate_code }
>
> END /* any END even trigger body END */

That should be documented in the BEGIN ... END documentation, not the
WHEN ... DO documentation.

Also, with the syntax you suggest, you'd only be able to have *one* and
only *one* WHEN block, which is not how it works.

That is, in the documentation of BEGIN ... END, it should probably
document it like:

<block> ::=
BEGIN
[<compound_statement> ...]
[<when-do> ...]
END

<compound_statement> ::= {<block> | <statement>}

Where <when-do> refers to the WHEN ... DO documentation, and maybe
repeat this in the section of WHEN ... DO.

Mark
--
Mark Rotteveel

Reply all
Reply to author
Forward
0 new messages