0

I wrote the following dynamic query in my stored procedure,

FETCH cur1 INTO a1, ldt1, b1;
    WHILE DONE = 0 DO

        SET @s=CONCAT('UPDATE ',fname , ' SET connectedDateTime = ldt1,opid = b1 
        WHERE hq_conferee_seqno=a1 AND (LoggedDateTime <=ldt1 AND connectedDateTime IS NULL)');
        FETCH cur1 INTO a1, ldt1, b1;
        PREPARE stmt FROM @s;
        EXECUTE stmt;
    END WHILE; 
    CLOSE cur1;

When I compile the stored I am not getting any error. But When I run this code I got an error like You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL' at line 1

What will be the problem. Please help me. Thanks in advance.

4

1 回答 1

0

手册所述:

局部变量仅在存储程序执行期间才在作用域内。在准备好的语句中不允许引用它们,因为它们对于当前会话是全局的,并且在执行语句时变量可能已经超出范围。

相反,您应该将光标获取到用户变量中,并从准备好的语句中引用那些(它们在连接期间保持在范围内),或者(更好)将它们作为参数传递给准备好的语句:

SET @s=CONCAT('UPDATE ',fname , ' SET connectedDateTime = ?,opid = ? 
WHERE hq_conferee_seqno=? AND (LoggedDateTime <=? AND connectedDateTime IS NULL)');
FETCH cur1 INTO @a1, @ldt1, @b1;
PREPARE stmt FROM @s;
EXECUTE stmt USING @ldt1, @b1, @a1, @ldt1;
于 2012-05-11T09:35:00.247 回答