问题是FETCH INTO(在循环中)没有将值放入变量中。我查看了MYSQL | SP | CURSOR - Fetch cursor into variable return null,但表格已经填充了。
交易表如下所示:
CREATE TABLE `transactionentry` (
`transactionid` bigint(20) NOT NULL AUTO_INCREMENT,
...
PRIMARY KEY (`transactionid`),
...
) ENGINE=InnoDB AUTO_INCREMENT=651 DEFAULT CHARSET=utf8;
存储过程:
PROCEDURE `doTxnHouseKeeping`()
BEGIN
-- Loop invariant
DECLARE noEntries INTEGER DEFAULT FALSE;
-- Error codes
DECLARE code CHAR(5) DEFAULT '00000';
DECLARE msg TEXT;
-- Txn vars
DECLARE transactionId BIGINT(20);
DECLARE lastTransactionId BIGINT(20) DEFAULT 0;
-- testing
DECLARE counter INT(11) DEFAULT 0;
DEClARE txnEntryCur CURSOR FOR
SELECT
`transactionid`
FROM
`transactionentry`
LIMIT 1;
DECLARE CONTINUE HANDLER FOR
NOT FOUND SET noEntries = TRUE;
DECLARE EXIT HANDLER FOR
SQLEXCEPTION
BEGIN
GET DIAGNOSTICS CONDITION 1
code = RETURNED_SQLSTATE, msg = MESSAGE_TEXT;
SELECT CONCAT('Error fetching transaction entries code: ', code, ' message: ', msg);
END;
OPEN txnEntryCur;
mainLoop: LOOP
FETCH
txnEntryCur
INTO
transactionId;
IF noEntries THEN
LEAVE mainLoop;
END IF;
IF transactionId IS NOT NULL THEN
INSERT INTO debugTable (`bigintval`) VALUES (transactionId);
ELSE
INSERT INTO debugTable (`strval`) VALUES ('transactionId is NULL');
END IF;
SET counter = counter + 1;
END LOOP mainLoop;
CLOSE txnEntryCur;
SELECT CONCAT("Count: ", counter);
END
运行存储过程会返回以下结果:
+--------------------------+
|CONCAT("Count: ", counter)|
+--------------------------+
| Count: 1|
+--------------------------+
调试表中的结果是:
+------------+---------+-----------------------+
|iddebugTable|bigintval| strval|
+------------+---------+-----------------------+
| 1| NULL|"transactionId is NULL"|
+------------+---------+-----------------------+
这意味着该值未在
中复制运行SQL时(就像在存储过程中一样),它返回:
+-------------+
|transactionid|
+-------------+
| 591|
+-------------+
答案 0 :(得分:7)
我发现了问题并且很奇怪。它不会导致任何错误和/或异常,只是不会将任何值放入变量中。解决方案是从:
更改游标声明语句DECLARE txnEntryCur CURSOR FOR
SELECT
`transactionid`
FROM
`transactionentry`
LIMIT 1;
要:
DECLARE txnEntryCur CURSOR FOR
SELECT
`transactionentry`.`transactionid`
FROM
`transactionentry`
LIMIT 1;
甚至文档都没有表明它可能是一个问题(findOne)
如果我从多个表中进行选择,那么我只完全限定了SQL语句的SELECT(和WHERE)部分,因此从未在更复杂的查询中选择它。
我希望将来可以节省一些时间。
答案 1 :(得分:0)
尝试为变量 if ["sad"] == ["triste"] {
print("match")
self.playerScore += 1
self.playerScoreLabel.text = String(self.playerScore)
}
提供默认值
transactionId
并替换
...
DECLARE transactionId BIGINT(20) DEFAULT 0
...
与
DECLARE noEntries INTEGER DEFAULT FALSE;
因为您希望将其用作DECLARE noEntries BOOLEAN DEFAULT FALSE;
值,并在稍后的过程中将其设置为BOOLEAN
。
答案 2 :(得分:0)
您的问题在这里:
DECLARE transactionId BIGINT(20);
您声明一个名为transactionId
的变量,因此在执行此操作时:
DEClARE txnEntryCur CURSOR FOR
SELECT
`transactionid`
FROM
`transactionentry`
LIMIT 1;
您的光标选择正在拾取您声明的变量,这就是为什么完全限定该字段起作用的原因。但是,如果您不想完全限定选择中的字段,则可以重命名变量。