0
我正在用InnoDB數據庫編寫一個小型瀏覽器遊戲。 爲了使發生的事情自動化,當你建造一所新房子時,我寫了下面的程序。它的問題是,除了「cycle_offset」之外,每個聲明的變量在第一個SELECT之後仍爲NULL,因此以下三個選擇無法在不傳遞NULL的情況下執行。一些變量保持爲空 - 存儲過程MySQL
DELIMITER //
DROP PROCEDURE IF EXISTS create_building //
CREATE PROCEDURE create_building(IN userID INT, IN buildingID INT)
BEGIN
--declare variables
DECLARE cycle_offset INT;
DECLARE res_1 INT;
DECLARE am_1 INT;
DECLARE res_2 INT;
DECLARE am_2 INT;
DECLARE res_3 INT;
DECLARE am_3 INT;
DECLARE u_am_1 INT;
DECLARE u_am_2 INT;
DECLARE u_am_3 INT;
--fill the values that belong to the certain type of building
SELECT cycles * 10,
res_1,
am_1,
res_2,
am_2,
res_3,
am_3
INTO cycle_offset,
res_1,
am_1,
res_2,
am_2,
res_3,
am_3
FROM build_cost
WHERE building = buildingID;
--select resources from user-fortune that were given in the build-cost
SELECT amount INTO u_am_1 FROM fortune WHERE resource = res_1;
SELECT amount INTO u_am_2 FROM fortune WHERE resource = res_2;
SELECT amount INTO u_am_3 FROM fortune WHERE resource = res_3;
--for each resource: if the resource is not null and the user has more resources left than needed
--remove the "price" from the user's fortune
IF
((res_1 IS NOT NULL) AND
(u_am_1 >= am_1))
THEN
UPDATE fortune
SET amount = (u_am_1 - am_1)
WHERE resource = res_1 AND
user = userID;
END IF;
IF
((res_1 IS NOT NULL) AND
(u_am_2 >= am_2))
THEN
UPDATE fortune
SET amount = (u_am_2 - am_2)
WHERE resource = res_2 AND
user = userID;
END IF;
IF
((res_1 IS NOT NULL) AND
(u_am_3 >= am_3))
THEN
UPDATE fortune
SET amount = (u_am_3 - am_3)
WHERE resource = res_3 AND
user = userID;
END IF;
--add the building into the users town
INSERT INTO town (user, building, cycle_start, level, in_construction)
VALUES (userID,
buildingID,
SEC_TO_TIME(FLOOR((TIME_TO_SEC(CURRENT_TIME)+5)/10) * 10 + cycle_offset),
1,
1);
--debugging purposes:
INSERT INTO debug (a,b,c,d,e,f,g,h,i,j,k,l) VALUES (userID, buildingID, cycle_offset, res_1, am_1, res_2, am_2, res_3, am_3, u_am_1, u_am_2, u_am_3);
END //
調試表總是向我顯示userID,buildingID和cycle_offset不爲空,剩下的是。你能想到的是,「build_cost」 - 表是空的,但是單獨運行查詢給了我這樣的結果(如預期):
SELECT cycles * 10, res_1, am_1, res_2, am_2, res_3, am_3
FROM build_cost
WHERE building = 1;
+-------------+-------+------+-------+------+-------+------+
| cycles * 10 | res_1 | am_1 | res_2 | am_2 | res_3 | am_3 |
+-------------+-------+------+-------+------+-------+------+
| 0 | 4 | 50 | 5 | 50 | NULL | NULL |
+-------------+-------+------+-------+------+-------+------+
爲什麼是週期,RES_1,am_1,RES_2的價值觀和am_2沒有存儲在我聲明的變量,如果我運行包含此查詢的過程?
我沒有看到你的代碼的任何地方,你要設置RES_3或am_3一個值。 –
'SELECT週期* 10, RES_1, am_1, RES_2, am_2, RES_3, am_3 INTO cycle_offset, RES_1, am_1, RES_2, am_2, RES_3, am_3 FROM build_cost WHERE building = buildingID;' 它應該發生在這裏,還是這是錯的? – cscholz
只有在這些列中有值時。 –