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

  • SEARCH
  • Home
  • 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 9140493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:31:24+00:00 2026-06-17T09:31:24+00:00

I want to trigger create new rows in b table when a row is

  • 0

I want to trigger create new rows in b table when a row is inserted to a table.

I have a table staff_leave_application like this:

id |  staff     |   leave type  | start date | end date       |  is half  |
---+------------+---------------+------------+----------------+-----------+
 1 | John Doe   |  Casual Leave | 17/01/2013 | 20/01/2013     |     0     |

if a row is inserted in this table, then it will trigger and insert to another table leave

the data to be inserted to leave table:

Split date range start date – end date to day, like :

17/01/2013 - 20/01/2013 will be

17/01/2013
18/01/2013
19/01/2013
20/01/2013

now insert per day to per row of leave table, along with staff_leave_applications column ID, staff, leave type.

I followed @Shaharsh shah’s answer and got this, thanks to Sharsh.

DELIMITER $$

USE `mydb`$$

CREATE
    /*!50017 DEFINER = 'root'@'localhost' */
    TRIGGER `tn_air_staff_leave_application` AFTER INSERT ON `staff_leave_application` 
    FOR EACH ROW BEGIN
        SET @counter := -1; 
        WHILE (@counter < DATEDIFF(DATE(new.end_date), DATE(new.start_date))) DO 
            INSERT INTO `leave`(staff_id_staff, leave_type_id_leave_type, staff_leave_application_id_staff_leave_application, leave_date) 
            VALUES (new.staff_id_staff, new.leave_type_id_leave_type, new.id_staff_leave_application, DATE_ADD(new.start_date, INTERVAL @counter:=@counter + 1 DAY)); 
        END WHILE; 
END;
$$
  • 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-17T09:31:25+00:00Added an answer on June 17, 2026 at 9:31 am

    Try this:

    DELIMITER $$
    
    DROP TRIGGER /*!50032 IF EXISTS */ `tn_air_staff_leave_application`$$
    
    CREATE
        /*!50017 DEFINER = 'root'@'localhost' */
        TRIGGER `tn_air_staff_leave_application` AFTER INSERT ON `staff_leave_application` 
        FOR EACH ROW BEGIN
            SET @counter := -1; 
            WHILE (@counter < DATEDIFF(DATE(new.endDate), DATE(new.startDate))) DO 
                INSERT INTO `leave`(staffId, leaveType, leaveDate) 
                VALUES (new.id, new.leaveType, DATE_ADD(new.startDate, INTERVAL @counter:=@counter + 1 DAY)); 
            END WHILE; 
    END;
    $$
    
    DELIMITER ;
    

    DELETE Trigger

    DELIMITER $$
    
    DROP TRIGGER /*!50032 IF EXISTS */ `tn_adr_staff_leave_application`$$
    
    CREATE
        /*!50017 DEFINER = 'root'@'localhost' */
        TRIGGER `tn_adr_staff_leave_application` AFTER DELETE ON `staff_leave_application` 
        FOR EACH ROW BEGIN
              DELETE FROM `leave` WHERE staffId = old.id;
    END;
    $$
    
    DELIMITER ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to PostgreSQL. I have tables like: CREATE TABLE Person ( ID SERIAL
I want to create a trigger on a SQL Server table. Table has 8
I just want an update trigger like this postgresql version... It seems to me
I want to create an Insert trigger that updates values on all the inserted
I have tow tables concept_access and concept_access_log. I want to create a trigger that
I have a trigger that I want to run before insert a row into
I have this trigger: create or replace trigger t_calctotal after insert or update on
we want to create a trigger which checks if a new measurement (=messung) point
I want to create a trigger on a table called takes in postgresql to
I want a SQL trigger to fire when a table is updated. I have

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.