我在MySQL中编写SP时遇到问题。根据我在网上可以阅读的内容,以下内容应该有效,但事实并非如此。我使用Mysql v.5.1.35
CREATE DEFINER=`My_Username`@`%` PROCEDURE `ChangePassword`(IN SPusername VARCHAR(100),
IN SPoldPassword VARCHAR(100),
IN SPnewPassword VARCHAR(100))
BEGIN
IF EXISTS(SELECT Password_Hash
FROM Customer
WHERE SPusername = Email
AND SPoldPassword = Password_Hashed)
THEN
UPDATE Customer
SET Password_Hashed = SPnewPassword
END IF;
END;
答案 0 :(得分:1)
我认为这是因为程序名称不应该在's。
在第一行中,试试这个:
CREATE DEFINER='My_Username'@'%' PROCEDURE ChangePassword(IN SPusername VARCHAR(100),
哦......还记得在UPDATE语句中查看有关WHERE子句的旁观者答案。如果不扩展WHERE子句,所有密码都会被设置为相同的东西......每一次!他建议很好! (虽然SQL语句可行):-)
问候
Sigersted
PS:完整的查询(带分隔符和旁观者的建议):
DELIMITER //
CREATE DEFINER='My_Username'@'%' PROCEDURE ChangePassword(
IN SPusername VARCHAR(100),
IN SPoldPassword VARCHAR(100),
IN SPnewPassword VARCHAR(100))
BEGIN
IF EXISTS(SELECT Password_Hash
FROM Customer
WHERE SPusername = Email
AND SPoldPassword = Password_Hashed)
THEN
UPDATE Customer
SET Password_Hashed = SPnewPassword
WHERE SPusername = Email
AND SPoldPassword = Password_Hashed;
END IF;
END//
DELIMITER ;
答案 1 :(得分:0)
看起来您可能会错过UPDATE语句中的IF EXISTS检查中的WHERE语句。
将其更改为
UPDATE Customer
SET Password_Hashed = SPnewPassword
WHERE SPusername = Email
AND SPoldPassword = Password_Hashed