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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:06:43+00:00 2026-06-17T16:06:43+00:00

I want to split rows using trigger, to 2 different tables if rows are

  • 0

I want to split rows using trigger, to 2 different tables if rows are more then 5 then insert only 5 rows to table leave and others rows to table nonpay_leave . I have a working trigger which insert rows only in one (I included trigger and my database code at the bottom of question).

currently working trigger

I have a table called staff_leave_application which have this columns :

id_staff_leave_application | staff_id_staff | leave_type_id_leave_type | start_date | end_date | joining_date

and it has a trigger called tn_air_staff_leave_application:

DELIMITER $$

USE `mydb`$$

CREATE
DEFINER=`root`@`localhost`
TRIGGER `mydb`.`tn_air_staff_leave_application`
AFTER INSERT ON `mydb`.`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_leave_application_id_staff_leave_application, staff_leave_application_staff_id_staff, leave_type_id_leave_type, date, active, date_updated) 
            VALUES (new.id_staff_leave_application, new.staff_id_staff, new.leave_type_id_leave_type, DATE_ADD(new.start_date, INTERVAL @counter:=@counter + 1 DAY), 1, CURDATE()); 
        END WHILE; 
END$$

what above trigger actually do?

This trigger run after insert on staff_leave_application. The trigger split currently inserted row in staff_leave_application by per day per row from date range and then insert rows to the table called leave each day per row.

Then, What i want actually ???

I want to change this trigger, so that if new rows are more then 5 then insert other rows to another table called nonpay_leave. like:
if trigger generates 7 rows, then insert first 5 rows to table leave and other 2 rows to table nonpay_leave.

My database structure

--
-- Table structure for table `designation`
--

CREATE TABLE IF NOT EXISTS `designation` (
  `id_designation` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(45) NOT NULL,
  PRIMARY KEY (`id_designation`),
  UNIQUE KEY `id_designation_UNIQUE` (`id_designation`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=14 ;

--
-- Dumping data for table `designation`
--

INSERT INTO `designation` (`id_designation`, `name`) VALUES
(10, 'Manager'),
(12, 'Medical Officer'),
(13, 'Peon');

-- --------------------------------------------------------

--
-- Table structure for table `leave`
--

CREATE TABLE IF NOT EXISTS `leave` (
  `id_leave` int(11) NOT NULL AUTO_INCREMENT,
  `staff_leave_application_id_staff_leave_application` int(11) NOT NULL,
  `staff_leave_application_staff_id_staff` int(11) NOT NULL,
  `leave_type_id_leave_type` int(11) NOT NULL,
  `date` date NOT NULL,
  `active` int(11) NOT NULL DEFAULT '1',
  `date_updated` date NOT NULL,
  PRIMARY KEY (`id_leave`,`staff_leave_application_id_staff_leave_application`,`staff_leave_application_staff_id_staff`),
  KEY `fk_table1_leave_type1` (`leave_type_id_leave_type`),
  KEY `fk_table1_staff_leave_application1` (`staff_leave_application_id_staff_leave_application`,`staff_leave_application_staff_id_staff`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=32 ;

--
-- Dumping data for table `leave`
--

INSERT INTO `leave` (`id_leave`, `staff_leave_application_id_staff_leave_application`, `staff_leave_application_staff_id_staff`, `leave_type_id_leave_type`, `date`, `active`, `date_updated`) VALUES
(21, 12, 7, 8, '2013-01-22', 1, '2013-01-21'),
(22, 12, 7, 8, '2013-01-23', 1, '2013-01-21'),
(23, 12, 7, 8, '2013-01-24', 1, '2013-01-21'),
(24, 12, 7, 8, '2013-01-25', 1, '2013-01-21'),
(25, 13, 7, 9, '2013-01-30', 1, '2013-01-21'),
(26, 13, 7, 9, '2013-01-31', 1, '2013-01-21'),
(27, 14, 7, 8, '2013-02-11', 1, '2013-01-21'),
(28, 14, 7, 8, '2013-02-12', 1, '2013-01-21'),
(29, 14, 7, 8, '2013-02-13', 1, '2013-01-21'),
(30, 14, 7, 8, '2013-02-14', 1, '2013-01-21'),
(31, 14, 7, 8, '2013-02-15', 1, '2013-01-21');

-- --------------------------------------------------------

--
-- Table structure for table `leave_allowed`
--

CREATE TABLE IF NOT EXISTS `leave_allowed` (
  `id_leave_allowed` int(11) NOT NULL AUTO_INCREMENT,
  `staff_id_staff` int(11) NOT NULL,
  `leave_type_id_leave_type` int(11) NOT NULL,
  `days` float NOT NULL,
  PRIMARY KEY (`id_leave_allowed`),
  UNIQUE KEY `id_leave_allowed_UNIQUE` (`id_leave_allowed`),
  KEY `fk_leave_allowed_staff1` (`staff_id_staff`),
  KEY `fk_leave_allowed_leave_type1` (`leave_type_id_leave_type`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `leave_allowed`
--

INSERT INTO `leave_allowed` (`id_leave_allowed`, `staff_id_staff`, `leave_type_id_leave_type`, `days`) VALUES
(1, 7, 8, 2.5),
(2, 7, 9, 200);

-- --------------------------------------------------------

--
-- Table structure for table `leave_app_view`
--

CREATE ALGORITHM=UNDEFINED DEFINER=`1`@`localhost` SQL SECURITY DEFINER VIEW `mydb`.`leave_app_view` AS select `mydb`.`staff`.`name` AS `name`,`mydb`.`staff`.`file_no` AS `file_no`,`mydb`.`staff`.`photo` AS `photo`,`mydb`.`staff_leave_application`.`leave_type_id_leave_type` AS `leave_type_id_leave_type`,`mydb`.`staff_leave_application`.`start_date` AS `start_date`,`mydb`.`staff_leave_application`.`end_date` AS `end_date`,`mydb`.`staff_leave_application`.`joining_date` AS `joining_date` from (`mydb`.`staff` join `mydb`.`staff_leave_application` on((`mydb`.`staff`.`id_staff` = `mydb`.`staff_leave_application`.`staff_id_staff`)));

--
-- Dumping data for table `leave_app_view`
--

INSERT INTO `leave_app_view` (`name`, `file_no`, `photo`, `leave_type_id_leave_type`, `start_date`, `end_date`, `joining_date`) VALUES
('Rahul Ayan', '54', NULL, 8, '2013-01-22', '2013-01-25', '2013-01-26'),
('Rahul Ayan', '54', NULL, 9, '2013-01-30', '2013-01-31', '2013-02-01'),
('Rahul Ayan', '54', NULL, 8, '2013-02-11', '2013-02-15', '2013-02-16');

-- --------------------------------------------------------

--
-- Table structure for table `leave_balance`
--

CREATE TABLE IF NOT EXISTS `leave_balance` (
  `id_leave_balance` int(11) NOT NULL AUTO_INCREMENT,
  `staff_id_staff` int(11) NOT NULL,
  `leave_type_id_leave_type` int(11) NOT NULL,
  `balance` int(3) NOT NULL,
  `date_added` date NOT NULL,
  PRIMARY KEY (`id_leave_balance`),
  UNIQUE KEY `id_leave_balance_UNIQUE` (`id_leave_balance`),
  KEY `fk_leave_balance_staff1` (`staff_id_staff`),
  KEY `fk_leave_balance_leave_type1` (`leave_type_id_leave_type`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `leave_balance`
--

INSERT INTO `leave_balance` (`id_leave_balance`, `staff_id_staff`, `leave_type_id_leave_type`, `balance`, `date_added`) VALUES
(1, 7, 8, 230, '2013-01-21'),
(2, 7, 9, 200, '2013-01-21');

-- --------------------------------------------------------

--
-- Table structure for table `leave_balance_view`
--

CREATE ALGORITHM=UNDEFINED DEFINER=`1`@`localhost` SQL SECURITY DEFINER VIEW `mydb`.`leave_balance_view` AS select `mydb`.`leave_balance`.`staff_id_staff` AS `staff_id_staff`,`leave_taken`.`leave_type_id_leave_type` AS `leave_type_id_leave_type`,(`mydb`.`leave_balance`.`balance` - `leave_taken`.`days`) AS `new_balance`,`mydb`.`leave_balance`.`date_added` AS `date_added` from ((`mydb`.`staff` join `mydb`.`leave_taken` on((`mydb`.`staff`.`id_staff` = `leave_taken`.`staff_leave_application_staff_id_staff`))) join `mydb`.`leave_balance` on(((`leave_taken`.`staff_leave_application_staff_id_staff` = `mydb`.`leave_balance`.`staff_id_staff`) and (`leave_taken`.`leave_type_id_leave_type` = `mydb`.`leave_balance`.`leave_type_id_leave_type`))));

--
-- Dumping data for table `leave_balance_view`
--

INSERT INTO `leave_balance_view` (`staff_id_staff`, `leave_type_id_leave_type`, `new_balance`, `date_added`) VALUES
(7, 8, 221, '2013-01-21'),
(7, 9, 198, '2013-01-21');

-- --------------------------------------------------------

--
-- Table structure for table `leave_taken`
--

CREATE ALGORITHM=UNDEFINED DEFINER=`1`@`localhost` SQL SECURITY DEFINER VIEW `mydb`.`leave_taken` AS select `mydb`.`leave`.`staff_leave_application_staff_id_staff` AS `staff_leave_application_staff_id_staff`,`mydb`.`leave`.`leave_type_id_leave_type` AS `leave_type_id_leave_type`,count(0) AS `days` from (`mydb`.`leave` join `mydb`.`staff` on((`mydb`.`staff`.`id_staff` = `mydb`.`leave`.`staff_leave_application_staff_id_staff`))) where (`mydb`.`leave`.`active` = 1) group by `mydb`.`leave`.`leave_type_id_leave_type`;

--
-- Dumping data for table `leave_taken`
--

INSERT INTO `leave_taken` (`staff_leave_application_staff_id_staff`, `leave_type_id_leave_type`, `days`) VALUES
(7, 8, 9),
(7, 9, 2);

-- --------------------------------------------------------

--
-- Table structure for table `leave_type`
--

CREATE TABLE IF NOT EXISTS `leave_type` (
  `id_leave_type` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(45) NOT NULL,
  `paid` int(11) DEFAULT NULL,
  PRIMARY KEY (`id_leave_type`),
  UNIQUE KEY `id_leave_type_UNIQUE` (`id_leave_type`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

--
-- Dumping data for table `leave_type`
--

INSERT INTO `leave_type` (`id_leave_type`, `name`, `paid`) VALUES
(8, 'Casual Leave', NULL),
(9, 'Medical Leave', NULL);

-- --------------------------------------------------------

--
-- Table structure for table `nonpay_leave`
--

CREATE TABLE IF NOT EXISTS `nonpay_leave` (
  `id_nonpay_leave` int(11) NOT NULL AUTO_INCREMENT,
  `staff_leave_application_id_staff_leave_application` int(11) NOT NULL,
  `staff_leave_application_staff_id_staff` int(11) NOT NULL,
  `leave_type_id_leave_type` int(11) NOT NULL,
  `leave_date` date DEFAULT NULL,
  `active` int(1) DEFAULT NULL,
  `date_updated` date DEFAULT NULL,
  PRIMARY KEY (`id_nonpay_leave`,`staff_leave_application_id_staff_leave_application`,`staff_leave_application_staff_id_staff`),
  KEY `fk_nonpay_leave_staff_leave_application1` (`staff_leave_application_id_staff_leave_application`,`staff_leave_application_staff_id_staff`),
  KEY `fk_nonpay_leave_leave_type1` (`leave_type_id_leave_type`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

--
-- Dumping data for table `nonpay_leave`
--


-- --------------------------------------------------------

--
-- Table structure for table `staff`
--

CREATE TABLE IF NOT EXISTS `staff` (
  `id_staff` int(11) NOT NULL AUTO_INCREMENT,
  `file_no` varchar(45) NOT NULL,
  `title_id_title` int(11) NOT NULL,
  `name` varchar(80) NOT NULL,
  `dob` date DEFAULT NULL,
  `designation_id_designation` int(11) NOT NULL,
  `status_id_status` int(11) NOT NULL,
  `date_of_join` date DEFAULT NULL,
  `photo` blob,
  `user_name` varchar(10) DEFAULT NULL,
  `password` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`id_staff`),
  UNIQUE KEY `id_staff_UNIQUE` (`id_staff`),
  KEY `fk_staff_title` (`title_id_title`),
  KEY `fk_staff_designation1` (`designation_id_designation`),
  KEY `fk_staff_status1` (`status_id_status`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;

--
-- Dumping data for table `staff`
--

INSERT INTO `staff` (`id_staff`, `file_no`, `title_id_title`, `name`, `dob`, `designation_id_designation`, `status_id_status`, `date_of_join`, `photo`, `user_name`, `password`) VALUES
(7, '54', 10, 'Rahul Ayan', '1991-09-14', 10, 6, '2012-01-02', NULL, NULL, NULL);

-- --------------------------------------------------------

--
-- Table structure for table `staff_leave_application`
--

CREATE TABLE IF NOT EXISTS `staff_leave_application` (
  `id_staff_leave_application` int(11) NOT NULL AUTO_INCREMENT,
  `staff_id_staff` int(11) NOT NULL,
  `leave_type_id_leave_type` int(11) NOT NULL,
  `start_date` date NOT NULL,
  `end_date` date NOT NULL,
  `joining_date` date NOT NULL,
  PRIMARY KEY (`id_staff_leave_application`,`staff_id_staff`),
  UNIQUE KEY `id_staff_leave_UNIQUE` (`id_staff_leave_application`),
  KEY `fk_staff_leave_staff1` (`staff_id_staff`),
  KEY `fk_staff_leave_leave_type1` (`leave_type_id_leave_type`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;

--
-- Dumping data for table `staff_leave_application`
--

INSERT INTO `staff_leave_application` (`id_staff_leave_application`, `staff_id_staff`, `leave_type_id_leave_type`, `start_date`, `end_date`, `joining_date`) VALUES
(12, 7, 8, '2013-01-22', '2013-01-25', '2013-01-26'),
(13, 7, 9, '2013-01-30', '2013-01-31', '2013-02-01'),
(14, 7, 8, '2013-02-11', '2013-02-15', '2013-02-16');

--
-- Triggers `staff_leave_application`
--
DROP TRIGGER IF EXISTS `tn_air_staff_leave_application`;
DELIMITER //
CREATE 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_leave_application_id_staff_leave_application, staff_leave_application_staff_id_staff, leave_type_id_leave_type, date, active, date_updated) 
            VALUES (new.id_staff_leave_application, new.staff_id_staff, new.leave_type_id_leave_type, DATE_ADD(new.start_date, INTERVAL @counter:=@counter + 1 DAY), 1, CURDATE()); 
        END WHILE; 
END
//
DELIMITER ;

-- --------------------------------------------------------

--
-- Table structure for table `status`
--

CREATE TABLE IF NOT EXISTS `status` (
  `id_status` int(11) NOT NULL AUTO_INCREMENT,
  `type` varchar(45) NOT NULL,
  PRIMARY KEY (`id_status`),
  UNIQUE KEY `id_ststus_UNIQUE` (`id_status`),
  UNIQUE KEY `type_UNIQUE` (`type`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;

--
-- Dumping data for table `status`
--

INSERT INTO `status` (`id_status`, `type`) VALUES
(6, 'Contract'),
(7, 'Permanent');

-- --------------------------------------------------------

--
-- Table structure for table `title`
--

CREATE TABLE IF NOT EXISTS `title` (
  `id_title` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(5) NOT NULL,
  PRIMARY KEY (`id_title`),
  UNIQUE KEY `id_title_UNIQUE` (`id_title`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=14 ;

--
-- Dumping data for table `title`
--

INSERT INTO `title` (`id_title`, `name`) VALUES
(10, 'Mr'),
(11, 'Dr'),
(12, 'Mrs'),
(13, 'Miss');

--
-- Constraints for dumped tables
--

--
-- Constraints for table `leave`
--
ALTER TABLE `leave`
  ADD CONSTRAINT `fk_table1_leave_type1` FOREIGN KEY (`leave_type_id_leave_type`) REFERENCES `leave_type` (`id_leave_type`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  ADD CONSTRAINT `fk_table1_staff_leave_application1` FOREIGN KEY (`staff_leave_application_id_staff_leave_application`, `staff_leave_application_staff_id_staff`) REFERENCES `staff_leave_application` (`id_staff_leave_application`, `staff_id_staff`) ON DELETE CASCADE ON UPDATE NO ACTION;

--
-- Constraints for table `leave_allowed`
--
ALTER TABLE `leave_allowed`
  ADD CONSTRAINT `fk_leave_allowed_leave_type1` FOREIGN KEY (`leave_type_id_leave_type`) REFERENCES `leave_type` (`id_leave_type`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  ADD CONSTRAINT `fk_leave_allowed_staff1` FOREIGN KEY (`staff_id_staff`) REFERENCES `staff` (`id_staff`) ON DELETE CASCADE ON UPDATE NO ACTION;

--
-- Constraints for table `leave_balance`
--
ALTER TABLE `leave_balance`
  ADD CONSTRAINT `fk_leave_balance_leave_type1` FOREIGN KEY (`leave_type_id_leave_type`) REFERENCES `leave_type` (`id_leave_type`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  ADD CONSTRAINT `fk_leave_balance_staff1` FOREIGN KEY (`staff_id_staff`) REFERENCES `staff` (`id_staff`) ON UPDATE NO ACTION;

--
-- Constraints for table `nonpay_leave`
--
ALTER TABLE `nonpay_leave`
  ADD CONSTRAINT `fk_nonpay_leave_staff_leave_application1` FOREIGN KEY (`staff_leave_application_id_staff_leave_application`, `staff_leave_application_staff_id_staff`) REFERENCES `staff_leave_application` (`id_staff_leave_application`, `staff_id_staff`) ON DELETE CASCADE ON UPDATE NO ACTION,
  ADD CONSTRAINT `fk_nonpay_leave_leave_type1` FOREIGN KEY (`leave_type_id_leave_type`) REFERENCES `leave_type` (`id_leave_type`) ON DELETE NO ACTION ON UPDATE NO ACTION;

--
-- Constraints for table `staff`
--
ALTER TABLE `staff`
  ADD CONSTRAINT `fk_staff_designation1` FOREIGN KEY (`designation_id_designation`) REFERENCES `designation` (`id_designation`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  ADD CONSTRAINT `fk_staff_status1` FOREIGN KEY (`status_id_status`) REFERENCES `status` (`id_status`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  ADD CONSTRAINT `fk_staff_title` FOREIGN KEY (`title_id_title`) REFERENCES `title` (`id_title`) ON DELETE NO ACTION ON UPDATE NO ACTION;

--
-- Constraints for table `staff_leave_application`
--
ALTER TABLE `staff_leave_application`
  ADD CONSTRAINT `fk_staff_leave_leave_type1` FOREIGN KEY (`leave_type_id_leave_type`) REFERENCES `leave_type` (`id_leave_type`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  ADD CONSTRAINT `fk_staff_leave_staff1` FOREIGN KEY (`staff_id_staff`) REFERENCES `staff` (`id_staff`) ON DELETE CASCADE ON UPDATE NO ACTION;

Updated Trigger

BEGIN
    set @loopcounter := 0;
    set @counter:= -1;
    set @balance:= (SELECT new_balance FROM leave_balance_view WHERE staff_id_staff = new.staff_id_staff and leave_type_id_leave_type = new.leave_type_id_leave_type);
   -- set @newrows:= (select count(*) from tn_air_staff_leave_application);

    WHILE ((@counter < DATEDIFF(DATE(new.end_date), DATE(new.start_date))) && (@loopcounter < @balance)) DO 
                    INSERT INTO `leave`(staff_leave_application_id_staff_leave_application, staff_leave_application_staff_id_staff, leave_type_id_leave_type, date, active, date_updated) 
                    VALUES (new.id_staff_leave_application, new.staff_id_staff, new.leave_type_id_leave_type, DATE_ADD(new.start_date, INTERVAL @counter:=@counter + 1 DAY), 1, CURDATE()); 
        set @loopcounter := @loopcounter + 1;
                END WHILE; 

    set @counter:=+(@balance - 1);
    set @loopcounter:=0;
    WHILE ((@counter < DATEDIFF(DATE(new.end_date), DATE(new.start_date))) && (@loopcounter < 2)) DO 
        INSERT INTO `nonpay_leave`(staff_leave_application_id_staff_leave_application, staff_leave_application_staff_id_staff, leave_type_id_leave_type, date, active, date_updated) 
        VALUES (new.id_staff_leave_application, new.staff_id_staff, new.leave_type_id_leave_type, DATE_ADD(new.start_date, INTERVAL @counter:=@counter + 1 DAY), 1, CURDATE()); 
        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-17T16:06:44+00:00Added an answer on June 17, 2026 at 4:06 pm
    set @loopcounter := 0;
    set @counter:= -1;
    
    WHILE ((@counter < DATEDIFF(DATE(new.end_date), DATE(new.start_date))) && (@loopcounter <5)) DO 
                INSERT INTO `leave`(staff_leave_application_id_staff_leave_application, staff_leave_application_staff_id_staff, leave_type_id_leave_type, date, active, date_updated) 
                VALUES (new.id_staff_leave_application, new.staff_id_staff, new.leave_type_id_leave_type, DATE_ADD(new.start_date, INTERVAL @counter:=@counter + 1 DAY), 1, CURDATE()); 
    set @loopcounter := @loopcounter + 1;
            END WHILE; 
    
    set @counter:=+4 (basically adjusting for the previous 5 that u marked in)
    set @loopcounter:=0;
    WHILE ((@counter < DATEDIFF(DATE(new.end_date), DATE(new.start_date))) && (@loopcounter <2)) DO 
    insert into NO_PAY_LEAVE
    
    • * the idea is a new loop_counter which goes to 5 before it breaks out of first loop.
    • * a new 2nd loop_counter which breaks at 2 or more (not sure wht the requirement is, maybe u wont need it in the 2nd loop)
    • * ur original counter adjusted to either +4 in the 2nd loop (not sure which one, sorta confused but i think it shud be +4 or it cud be
      -6)

    Ok i have edited and think this is what the new logic shud b.. many counters have changed


    well alrite i get u, now u will have to do what u said.. before u enter any loop.. loop1 (max times it can execute is @balance times).. loop2 (it shud compulsarily execute the remainder times).. i think u shud set a new counter called leave_counter in the beginning itself which will determine how many times the Leave Loop will execute..

    BEGIN
    
            set @loopcounter := 0;
            set @counter:= -1;
            set @balance:= (SELECT new_balance FROM leave_balance_view WHERE staff_id_staff = new.staff_id_staff and leave_type_id_leave_type = new.leave_type_id_leave_type);
        set @leavecounter:=+(@balance - 1);
    
    
           -- set @newrows:= (select count(*) from tn_air_staff_leave_application);
    
            WHILE ((@counter < DATEDIFF(DATE(new.end_date), DATE(new.start_date))) && (@loopcounter < @balance) && (@balance>0)) DO 
                            INSERT INTO `leave`(staff_leave_application_id_staff_leave_application, staff_leave_application_staff_id_staff, leave_type_id_leave_type, date, active, date_updated) 
                            VALUES (new.id_staff_leave_application, new.staff_id_staff, new.leave_type_id_leave_type, DATE_ADD(new.start_date, INTERVAL @counter:=@counter + 1 DAY), 1, CURDATE()); 
                set @loopcounter := @loopcounter + 1;
            set @balance := @balance - 1;
                        END WHILE; 
    
            WHILE ((@leavecounter < DATEDIFF(DATE(new.end_date), DATE(new.start_date)))) DO 
                INSERT INTO `nonpay_leave`(staff_leave_application_id_staff_leave_application, staff_leave_application_staff_id_staff, leave_type_id_leave_type, date, active, date_updated) 
                VALUES (new.id_staff_leave_application, new.staff_id_staff, new.leave_type_id_leave_type, DATE_ADD(new.start_date, INTERVAL @leavecounter:=@leavecounter + 1 DAY), 1, CURDATE()); 
                end while;
        END$$
    

    BOTTOMLINE::::::
    i have edited ur sql command by introducing a new counter called leave_counter which will determine how many times ur leave_loop shud run.. also in the first loop i have made sure that it does not execute more than @balance times by introducing the @balance in the first loop.. u can see the edited reply.. Loop 1: Executes max (@balance) times… Loop 2: Executes (no of days leave taken) – (@balance) times …

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

Sidebar

Related Questions

I want to read all rows in a table using system.data.sqlite. As I have
I'm trying to paginate a table using jquery. I want to split xxxx number
I want to split a string in Javascript using split function into 2 parts.
We are using mysql. We have a huge table which contains 1 billion rows.
I'm operating with huge CSV files (20-25Mln rows) and don't want to split them
I have a csv file that has rows that I want to split by
I want to send a file via email using c# ASP.net (Maybe more files
I have code like that 010200345 i want split every first three digit please
I want to split a file(suppose a mp3) into four parts.I have tried this
I want to split a series of divs vertically: My HTML might look like

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.