【问题标题】:MySQL trigger to create invoice on the 2nd of every monthMySQL trigger to create invoice on the 2nd of every month
【发布时间】:2022-12-02 08:22:40
【问题描述】:

So, I have two tables one Invoice and two invPerConn(invoice per connection) the invoice table needs a trigger that inserts a new invoice every 2nd of the next month and add a new due date or the 5th of that corresponding month. when the insert happens the trigger needs to insert "line_price" from invPerConn into invoices "amount_due_left" as well as "overdue_fee" from invPerConn

create table invoice(
invoice_id DECIMAL(3),
invoice_date DATE,
due_date DATE,
overdue_fee DECIMAL(10,2),
amt_due_left decimal(12,2),
PRIMARY KEY(invoice_id));

INSERT INTO invoice VALUES 
(1,'2020-11-02','2020-11-05',15,120.24),
(2,'2020-11-02','2020-11-05',35,200.00),
(3,'2020-11-02','2020-11-05',150,1300.00),
(4,'2020-11-02','2020-11-05',120,1200.00);


create table invPerConn(
cust_connection int,
invoice_id  DECIMAL(3),
line_price decimal(12,2),
overdue_fee DECIMAL(10,2),
CONSTRAINT fk_has_custConnection FOREIGN KEY(cust_connection)
        REFERENCES custConnection(cust_connection),
CONSTRAINT fk_has_invoice FOREIGN KEY(invoice_id)
        REFERENCES invoice(invoice_id));
    

insert into invPerConn values
(1,1,15,120.24),
(2,2,35,200.00),
(3,3,150,1300.00),
(4,4,120,1300.00);

I really don't know where to start with this one I haven't ever worked with a trigger that generates a date.

【问题讨论】:

  • that is not a trigger but instead an event and an activated event handler

标签: mysql sql mysql-workbench


【解决方案1】:
CREATE TRIGGER insert_invoice
AFTER INSERT ON invPerConn
FOR EACH ROW
BEGIN
    -- Calculate the next month's 2nd day
    DECLARE next_month_date DATE = DATEADD(MONTH, 1, CURRENT_DATE);
    SET next_month_date = DATEADD(DAY, 1, next_month_date);

    -- Calculate the next month's 5th day
    DECLARE next_month_due_date DATE = DATEADD(MONTH, 1, CURRENT_DATE);
    SET next_month_due_date = DATEADD(DAY, 4, next_month_due_date);

    -- Insert a new invoice for the next month with the calculated dates
    INSERT INTO invoice (invoice_id, invoice_date, due_date, overdue_fee, amt_due_left)
    VALUES (NEW.invoice_id, next_month_date, next_month_due_date, NEW.overdue_fee, NEW.line_price);
END;

This trigger uses the DATEADD function to calculate the next month's 2nd and 5th days, and then inserts a new row into the invoice table with these dates and the overdue_fee and line_price values from the invPerConn table.

You can modify this trigger to suit your specific requirements, such as inserting the new invoice with a different set of values, or using a different trigger event (e.g. AFTER UPDATE instead of AFTER INSERT). You can also add additional logic to the trigger to handle any errors or exceptional cases that may arise.

【讨论】:

    猜你喜欢
    • 2018-11-30
    • 2022-12-19
    • 2022-12-02
    • 2022-12-26
    • 2022-12-01
    • 2022-12-27
    • 2022-12-02
    • 2022-12-01
    • 2022-11-09
    相关资源
    最近更新 更多