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 4576968
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:16:49+00:00 2026-05-21T20:16:49+00:00

I have the following trigger on a blackhole table, that intercepts inserts and passes

  • 0

I have the following trigger on a blackhole table, that intercepts inserts and passes them on to other tables.
In order to speed things up I want to calculate an increasing value and passes that in my inserts.

DELIMITER $$

CREATE TRIGGER ai_blackhole_each AFTER INSERT ON `test.blackhole` FOR EACH ROW
BEGIN
  DECLARE calculated_id INTEGER;

  SET calculated_id = calc_id_for_previous_insert + 1;
  INSERT INTO example VALUES(new.field1, new.field2, calculated_id, ..);
END$$

DELIMITER ;

Can I have a static variable inside a trigger that keeps its value between firings?
Or is there a trick to achieve something like that efficiently?

  • 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-05-21T20:16:50+00:00Added an answer on May 21, 2026 at 8:16 pm

    Place an intermittent value in a MEMORY table starting at 0

    use test
    DROP TABLE IF EXISTS test.blackholecounter;
    DROP TABLE IF EXISTS test.blackhole;
    DROP TABLE IF EXISTS test.example;
    CREATE TABLE test.blackholecounter (calc_id INT NOT NULL DEFAULT 0) ENGINE=MEMORY;
    INSERT INTO test.blackholecounter VALUES (0);
    CREATE TABLE test.blackhole
    (
        field1 VARCHAR(20),
        field2 VARCHAR(20)
    ) ENGINE=BLACKHOLE;
    CREATE TABLE test.example (field1 VARCHAR(20),
    field2 VARCHAR(20),
    calc_id INT);
    DELIMITER $$
    CREATE TRIGGER ai_blackhole_each AFTER INSERT ON test.blackhole
    FOR EACH ROW
    BEGIN
        DECLARE calculated_id INTEGER;
        SELECT calc_id INTO calculated_id FROM test.blackholecounter;
        UPDATE test.blackholecounter SET calc_id=calc_id+1;
        INSERT INTO test.example VALUES(new.field1, new.field2, calculated_id);
    END
    $$
    DELIMITER ;
    SELECT * FROM test.blackholecounter;
    SELECT * FROM test.example;
    INSERT INTO test.blackhole (field1,field2) VALUES ('rolando','edwards'),('pamela','edwards');
    SELECT * FROM test.blackholecounter;
    SELECT * FROM test.example;
    

    Here is what I got when I pasted this into MySQL

    mysql> use test
    Database changed
    mysql> DROP TABLE IF EXISTS test.blackholecounter;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> DROP TABLE IF EXISTS test.blackhole;
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> DROP TABLE IF EXISTS test.example;
    Query OK, 0 rows affected (0.06 sec)
    
    mysql> CREATE TABLE test.blackholecounter (calc_id INT NOT NULL DEFAULT 0) ENGINE=MEMORY;
    Query OK, 0 rows affected (0.05 sec)
    
    mysql> INSERT INTO test.blackholecounter VALUES (0);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> CREATE TABLE test.blackhole(field1 VARCHAR(20),field2 VARCHAR(20)) ENGINE=BLACKHOLE;
    Query OK, 0 rows affected (0.06 sec)
    
    mysql> CREATE TABLE test.example (field1 VARCHAR(20),field2 VARCHAR(20),calc_id INT);
    Query OK, 0 rows affected (0.09 sec)
    
    mysql> DELIMITER $$
    mysql> CREATE TRIGGER ai_blackhole_each AFTER INSERT ON test.blackhole
        -> FOR EACH ROW
        -> BEGIN
        ->     DECLARE calculated_id INTEGER;
        ->     SELECT calc_id INTO calculated_id FROM test.blackholecounter;
        ->     UPDATE test.blackholecounter SET calc_id=calc_id+1;
        ->     INSERT INTO test.example VALUES(new.field1, new.field2, calculated_id);
        -> END
        -> $$
    Query OK, 0 rows affected (0.11 sec)
    
    mysql> DELIMITER ;
    mysql> SELECT * FROM test.blackholecounter;
    +---------+
    | calc_id |
    +---------+
    |       0 |
    +---------+
    1 row in set (0.00 sec)
    
    mysql> SELECT * FROM test.example;
    Empty set (0.02 sec)
    
    mysql> INSERT INTO test.blackhole (field1,field2) VALUES ('rolando','edwards'),('pamela','edwards');
    Query OK, 2 rows affected (0.07 sec)
    Records: 2  Duplicates: 0  Warnings: 0
    
    mysql> SELECT * FROM test.blackholecounter;
    +---------+
    | calc_id |
    +---------+
    |       2 |
    +---------+
    1 row in set (0.00 sec)
    
    mysql> SELECT * FROM test.example;
    +---------+---------+---------+
    | field1  | field2  | calc_id |
    +---------+---------+---------+
    | rolando | edwards |       0 |
    | pamela  | edwards |       1 |
    +---------+---------+---------+
    2 rows in set (0.00 sec)
    

    You can start the initial value in the test.blackholecounter table with some other number or change the order when the increment happens in the trigger.

    Give it a Try !!!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that appends some rows to a table. One of the
Let say I have the following desire, to simplify the IConvertible's to allow me
I have a project that adds elements to an AutoCad drawing. I noticed that
I have a new web app that is packaged as a WAR as part
My question is about memory use and objects in actionscript 2. If I have
I have several USB mass storage flash drives connected to a Ubuntu Linux computer
I have a snippet to create a 'Like' button for our news site: <iframe
I have found this example on StackOverflow: var people = new List<Person> { new
I am attempting to pull some information from my tnsnames file using regex. I
After having read Ian Boyd 's constructor series questions ( 1 , 2 ,

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.