我在这里做错了什么?我一直在说:
#1064 - 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 'INTO userID,
currentStatus, failedLogin FROM access WHERE username = p_UserName ' at line 9
当然,这是我的惯例:
DELIMITER $$
DROP PROCEDURE IF EXISTS `user_login`$$
CREATE PROCEDURE `user_login`(IN v_UserName VARCHAR(45), IN v_Password VARCHAR(45), IN v_IP VARCHAR(45), IN v_Source VARCHAR(45))
BEGIN
DECLARE userID INT(10);
DECLARE currentStatus VARCHAR(10);
DECLARE failedLogin INT(4);
IF EXISTS(SELECT access_user_id, status, failed_login_count INTO userID, currentStatus, failedLogin FROM access WHERE username = v_UserName AND hash = v_Password LIMIT 1) THEN
UPDATE access SET failed_login_count=0 WHERE access_user_id=userID;
INSERT INTO last_login (profiles_user_id, timestamp, ip_address, login_source) VALUES (userID, NOW(), v_IP, v_Source) ON DUPLICATE KEY UPDATE timestamp=VALUES(timestamp), ip_address=VALUES(ip_address), login_source=VALUES(login_source);
INSERT INTO login_history (profiles_user_id, timestamp, ip_address, login_source) VALUES (userID, NOW(), v_IP, v_Source);
CASE currentStatus
WHEN 'Active' THEN
SELECT userID, currentStatus;
WHEN 'Dormant' THEN
UPDATE access SET status='Active' WHERE access_user_id=userID;
SELECT userID, currentStatus;
WHEN 'Locked' THEN
SELECT userID, currentStatus;
ELSE
END CASE;
ELSE
IF EXISTS(SELECT access_user_id, failed_login_count INTO userID, failedLogin FROM access WHERE username = p_UserName LIMIT 1) THEN
failedLogin = failedLogin +1;
UPDATE access SET failed_login_count=failedLogin WHERE access_user_id=userID;
CASE failedLogin
WHEN (<10) THEN
SELECT 0, 'Invalid';
WHEN (>10) THEN
UPDATE access SET status='Locked' WHERE access_user_id=userID;
SELECT 0, 'Invalid';
ELSE
UPDATE access Set failed_login_count=1 WHERE access_user_id=userID;
SELECT 0, 'Invalid';
END CASE;
ELSE
SELECT 0, 'Invalid';
END IF;
END IF;
END$$
DELIMITER ;
我已经挖掘了MySQL 5.5文档并根据它,我的SELECT INTO语法是正确的......好吧,显然不是根据服务器...任何人都有任何想法?
答案 0 :(得分:0)
存在一些问题:
在SELECT...INTO
子句中使用EXISTS
语句。你不能这样做因为SELECT ... INTO不返回结果集,它只是设置变量。解决方法(在IF
语句中拆分查询并检查ID值):
SELECT access_user_id, status, failed_login_count INTO userID, currentStatus, failedLogin FROM access WHERE username = p_UserName AND hash = p_Password LIMIT 1;
IF userID IS NOT NULL THEN
...
从第一个CASE结构中删除ELSE
关键字。
上一个CASE
结构有错误的语法,请尝试这个 -
CASE
WHEN failedLogin < 10 THEN
SELECT 0, 'Invalid';
WHEN failedLogin > 10 THEN
UPDATE access SET status='Locked' WHERE access_user_id=userID;
SELECT 0, 'Invalid';
ELSE
UPDATE access Set failed_login_count=1 WHERE access_user_id=userID;
SELECT 0, 'Invalid';
END CASE;