【问题标题】:How to create temp table with select query update it and the insert it in mysql?如何使用选择查询创建临时表更新它并将其插入 mysql?
【发布时间】:2017-02-24 13:17:09
【问题描述】:

谁能告诉我为什么这不起作用

Create Procedure LeaveUpdate()
Begin
CREATE TEMPORARY TABLE leavebal AS (SELECT * FROM tbl_leave_balance WHERE month = month(DATE_SUB(now(), INTERVAL 1 MONTH)) and year = year(DATE_SUB(now(), INTERVAL 1 MONTH)));
UPDATE leavebal SET leave_balance = 0 WHERE leave_balance < 0;
UPDATE leavebal  SET month = month(now()), year = year(now()), leaves_taken = 0, carry_forward = leave_balance, comp_off = 0;
UPDATE leavebal SET total_leaves = carry_forward + leaves_allowed + comp_off, leave_balance = carry_forward + leaves_allowed + comp_off;
INSERT INTO tbl_leave_balance (emp_id, month, year, carry_forward, leaves_allowed, comp_off, total_leaves, leaves_taken, leave_balance)
SELECT emp_id, month, year, carry_forward, leaves_allowed, comp_off, total_leaves, leaves_taken, leave_balance FROM leavebal;
End

【问题讨论】:

  • 欢迎来到 Stack Overflow,很遗憾您的问题缺乏信息。我建议您阅读How do I ask a good question?,然后回来编辑您的问题,为我们提供所有必需的信息。否则,我们无法为您提供帮助,您的问题可能会被关闭。

标签: mysql stored-procedures temp-tables


【解决方案1】:

你必须使用delimiter,试试这个:

DELIMITER $$

    DROP PROCEDURE IF EXISTS `LeaveUpdate` $$
Create Procedure LeaveUpdate()
Begin
CREATE TEMPORARY TABLE leavebal AS (SELECT * FROM tbl_leave_balance WHERE month = month(DATE_SUB(now(), INTERVAL 1 MONTH)) and year = year(DATE_SUB(now(), INTERVAL 1 MONTH)));
UPDATE leavebal SET leave_balance = 0 WHERE leave_balance < 0;
UPDATE leavebal  SET month = month(now()), year = year(now()), leaves_taken = 0, carry_forward = leave_balance, comp_off = 0;
UPDATE leavebal SET total_leaves = carry_forward + leaves_allowed + comp_off, leave_balance = carry_forward + leaves_allowed + comp_off;
INSERT INTO tbl_leave_balance (emp_id, month, year, carry_forward, leaves_allowed, comp_off, total_leaves, leaves_taken, leave_balance)
SELECT emp_id, month, year, carry_forward, leaves_allowed, comp_off, total_leaves, leaves_taken, leave_balance FROM leavebal;
End$$

    DELIMITER ;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-08
    • 2010-10-24
    • 2018-02-25
    • 1970-01-01
    • 2014-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多