2017-02-24 83 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 ; 
相關問題