Variables Access in Exception Block.

Answered

Comments

2 comments

  • Comment actions Permalink
    Official comment
    Avatar
    Niklas Schmidtmer

    Hi Chris,

     

    thanks for your request. Variables are local to the block in which they were defined. Let's consider this example:

    BEGIN
    DECLARE STRING myVar = 'demo';
    EXCEPTION e
    SELECT myVar;
    END;;

    This snippet throws an error as two blocks are defined here: 1) between BEGIN and EXCEPTION, and 2) between EXCEPTION and END. Variables defined in the first block are not visible in the second one.

     

    If you wish to make variables visible in a broader scope, you can use nested blocks. This example works:

    BEGIN
    DECLARE STRING myVar;

    BEGIN
    myVar = 'demo';
    RAISE SQLEXCEPTION 123;
    EXCEPTION e
    SELECT myVar;
    END
    END;;

     

    Best regards

     

    Niklas

  • 0
    Comment actions Permalink
    Avatar
    Chris Shortt

    Hi Niklas,

    Ok. Not ideal, but easy enough to work with.

    Thanks.

    Chris

Please sign in to leave a comment.

Powered by Zendesk