2017-02-24 240 views
-2

谁能告诉我,为什么这不起作用如何使用select查询创建临时表并将其插入到mysql中?

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 
+1

欢迎堆栈溢出,不幸的是你的问题是缺乏信息。我建议你阅读[我如何问一个好问题?](http://stackoverflow.com/help/how-to-ask),然后回来编辑你的问题,给我们所有必要的信息。没有这些,我们不能帮助你,你的问题可能会被封闭。 – Styphon

回答

0

必须使用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 ; 
相关问题