Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7818881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:43:43+00:00 2026-06-02T06:43:43+00:00

I am trying to only execute a block of code if a variable exists.

  • 0

I am trying to only execute a block of code if a variable exists. Here is a code snipit. Can you nest Begin…End statements in an IF block?

I’ve re-designed this several times. Suggestions?

delimiter //


drop trigger if exists example_trigger;//
create trigger example_trigger AFTER UPDATE on some_table for each row 
BLOCK1: begin

    -- check current status
    DECLARE done BOOLEAN DEFAULT FALSE;


    -- cap check
    if (new.CURRENT_ALLOCATED >= new.TOTAL_ALLOWED_QTY) then
        SET done = TRUE;
    end if; -- cap check end


    -- o 
    if (done != TRUE and new.O_KEY is not null and new.A_KEY is null) then

        OBLOCK: begin
            DECLARE done_o BOOLEAN DEFAULT FALSE;
            DECLARE pd_nbr INT;
            DECLARE no_more_rows BOOLEAN;
            DECLARE cur_pd CURSOR FOR 
                select pd.STATUS_KEY
                from PROD_DEL_V pd
                    join PROD_T p on pd.KEY_NBR = p.KEY_NBR
                where pd.STATUS not in ('PU', 'EX')
                    and p.O_KEY = new.O_KEY;
            DECLARE CONTINUE HANDLER FOR NOT FOUND
                SET done_o = TRUE;  

            -- run updates
            if (done_o != TRUE) then

                -- open cursor
                OPEN cur_pd;        

                -- loop start
                loop_it: LOOP

                    FETCH cur_pd INTO pd_nbr;

                    -- exit loop if..
                    if no_more_rows = TRUE THEN 
                        CLOSE cur_pd;
                        LEAVE loop_it;
                    end if;


                    INSERT INTO STATUS_TABLE (
                        STATUS_KEY
                        , STATUS
                        , NOTE_TXT
                        )
                        (
                        SELECT 
                            PD.STATUS_KEY
                            , 'PU' AS STATUS
                            , concat('example_trigger - MAX has been reached or exceeded [TOTAL_ALLOWED_QTY = ',new.TOTAL_ALLOWED_QTY,' and CURRENT_ALLOCATED = ', new.CURRENT_ALLOCATED, ']') AS NOTE_TXT
                        FROM  PROD_DEL_TABLE PD 
                        WHERE PD.STATUS_KEY = pd_nbr
                        );  

                END LOOP loop_it;

            end if; -- run updates end

        end OBLOCK:; -- end block

    end if; -- o 


    -- a
    if (done != TRUE and new.O_KEY is null and new.A_KEY is not null) then

        ABLOCK: begin
            DECLARE done_a BOOLEAN DEFAULT FALSE;
            DECLARE pd_nbr INT;
            DECLARE no_more_rows BOOLEAN;
            DECLARE cur_pd CURSOR FOR 
                select pd.STATUS_KEY
                from PROD_DEL_V pd
                    join PROD_T p on pd.KEY_NBR = p.KEY_NBR
                    join A_O_T a on a.O_KEY = p.O_KEY
                where pd.STATUS not in ('PU', 'EX')
                    and a.A_KEY = new.A_KEY;
            DECLARE CONTINUE HANDLER FOR NOT FOUND
                SET done_a = TRUE;

            -- run updates
            if (done_a != TRUE) then

                -- open cursor
                OPEN cur_pd;        

                -- loop start
                loop_it: LOOP

                    FETCH cur_pd INTO pd_nbr;

                    -- exit loop if..
                    if no_more_rows = TRUE THEN 
                        CLOSE cur_pd;
                        LEAVE loop_it;
                    end if;


                    INSERT INTO STATUS_TABLE (
                        STATUS_KEY
                        , STATUS
                        , NOTE_TXT
                        )
                        (
                        SELECT 
                            PD.STATUS_KEY
                            , 'PU' AS STATUS
                            , concat('example_trigger - MAX has been reached or exceeded [TOTAL_ALLOWED_QTY = ',new.TOTAL_ALLOWED_QTY,' and CURRENT_ALLOCATED = ', new.CURRENT_ALLOCATED, ']' AS NOTE_TXT
                        FROM  PROD_DEL_TABLE PD 
                        WHERE PD.STATUS_KEY = pd_nbr
                        );  

                END LOOP loop_it;

            end if; -- run updates end

        end ABLOCK; -- end block

    end if; -- a
end BLOCK1; -- end large block
//

delimiter ;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:43:44+00:00Added an answer on June 2, 2026 at 6:43 am

    What is the problem with IF and BEGIN…END clause? Take a look at this simple example –

    CREATE PROCEDURE procedure1(IN Param1 VARCHAR(255))
    BEGIN
      IF Param1 = 1 THEN
        BEGIN
          DECLARE i INT;
          -- do something
        END;
      ELSE
        BEGIN
          DECLARE i INT;
          -- do something
        END;
      END IF;
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While trying to execute the following lines only the last two statements are displayed(Here
I'm trying to create a custom template tag that only renders a block of
I am trying to use a closure to ensure that a function can only
I'm trying to execute a asp.net webservice using jquery. When I pass only one
I'm trying to execute jQuery code when a div's style changes from display: none;
I'm trying to only cURL parts of an array generated by a game API.
I'm trying to minify multiple CSS files using preg_replace() . Actually, I'm only trying
This may sound like a stupid question, perhaps it is. But I'm only trying
I’ve only been trying it in Firefox’s JavaScript console, but neither of the following
I'm trying to match only numbers and spaces in my php regex but the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.