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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:48:41+00:00 2026-05-23T15:48:41+00:00

I’ve read that this can be done without issue using MyISAM as it is

  • 0

I’ve read that this can be done without issue using MyISAM as it is the default behavior , but I’m using InnoDB so need a trigger for such.

The two PK fields are batch and lineItem. If a record is deleted I want the numbering to start from the largest integer for batch. Not fill in the holes.

This is to set up a testing environment for a legacy system. So the schema is the way it is, I thought I’d mention that to avoid any discussion about whether it is good or not.

Edit: I want something like the following insert statement as a trigger

INSERT INTO payroll(`batch`,`lineItem`)
(select 'T105',t1.lineItem + 1 from payroll as t1 where batch = 'T105' order by lineItem desc limit 1);

But where ‘T105’ (the batch id) is hard coded I want the trigger to pick that up from the insert.

So I want to be able to say something like:

INSERT INTO payroll(`batch`)VALUES('T001','T001','T001', 'T002', 'T002', 'T002');

and I would expect to see in the table:

batch    lineItem
T001     1
T001     2
T001     3
T002     1
T002     2
T002     3

Getting further:

In trying to implement this I’ve come up with:

DELIMITER $$
CREATE TRIGGER `co05_test`.`ins_lineItem`
BEFORE INSERT ON `co05_test`.`my_table`
FOR EACH ROW
BEGIN

     select lineItem + 1 into @newLineItem from my_table where batch = NEW.batch order by lineItem desc limit 1;
     set NEW.lineItem = @newLineItem;   
END$$

However when I try…

INSERT INTO `co05_test`.`my_table`(`batch`)VALUES('T001');

I get this error: Column ‘lineItem’ cannot be null

Which is defined as not being nullable but I though the trigger should set the value!

Solution which I used:

-- Trigger DDL Statements
DELIMITER $$

USE `co05_test`$$

CREATE TRIGGER `co05_test`.`ins_lineItem`
BEFORE INSERT ON `co05_test`.`my_table`
FOR EACH ROW
BEGIN
     select count(*) into @batchCount from my_table where batch = NEW.batch;
     select lineItem + 1 into @newLineItem from my_table where batch = NEW.batch order by lineItem desc limit 1;
    if @batchCount > 0 then
       set NEW.lineItem = @newLineItem;
     else
       set NEW.lineItem = 1;
     end if;
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-05-23T15:48:42+00:00Added an answer on May 23, 2026 at 3:48 pm

    Have you tried declaring the variable instead?

    DELIMITER $$
    CREATE TRIGGER `co05_test`.`ins_lineItem`
    BEFORE INSERT ON `co05_test`.`my_table`
    FOR EACH ROW
    BEGIN
    DECLARE newLineItem INT; 
    SELECT 
        lineItem + 1 into newLineItem 
    FROM my_table 
    WHERE batch = NEW.batch
    ORDER BY lineItem DESC 
    LIMIT 1;
    
    SET NEW.lineItem = newLineItem;   
    END$$
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a French site that I want to parse, but am running into
I need to clean up various Word 'smart' characters in user input, including but
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is 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.