我是一名MySQL新手并且一直在尝试创建存储过程。以下代码返回错误Error Code: 1172. Result consisted of more than one row
。我究竟做错了什么? (我正在使用MySQL工作台)
CREATE DEFINER=`root`@`localhost` PROCEDURE `season_private_league_user`(
IN user_id INT,
OUT league_name VARCHAR(25),
OUT host_user VARCHAR(30))
BEGIN
DECLARE userteamid INT;
DECLARE var_league_name VARCHAR(25);
DECLARE var_host_user VARCHAR(30);
# Retrieve user team from user_id
SELECT CS_USER_TEAMS_ID INTO userteamid
FROM classicseasonmodel_classicseasonuserteam
WHERE user_id = user_id;
#LEAGUE NAME
SELECT classicseasonmodel_classicseasonprivateleague.private_league_name INTO var_league_name
FROM classicseasonmodel_classicseasonuserteamprivateleague
INNER JOIN classicseasonmodel_classicseasonprivateleague
ON classicseasonmodel_classicseasonuserteamprivateleague.private_league_id=classicseasonmodel_classicseasonprivateleague.CS_PRIVATE_LEAGUE_ID
WHERE user_team_id = userteamid;
#HOST_USER
SELECT classicseasonmodel_classicseasonprivateleague.host_user_id INTO var_host_user
FROM classicseasonmodel_classicseasonuserteamprivateleague
INNER JOIN classicseasonmodel_classicseasonprivateleague
ON classicseasonmodel_classicseasonuserteamprivateleague.private_league_id=classicseasonmodel_classicseasonprivateleague.CS_PRIVATE_LEAGUE_ID
WHERE user_team_id = userteamid;
SET league_name = var_league_name;
SET host_user = var_host_user;
END
CALL season_private_league_user(2, @league_name, @host_user);
SELECT @league_name AS league_name;
SELECT @host_user AS host_user;
答案 0 :(得分:1)
将结果存储在变量中的存储过程的SELECT之一返回多行,该行返回此错误。这样,您只能在变量中存储单个值,而不能存储多个值。
您可以阅读SELECT ... INTO语句here。对你来说最有趣的部分是:
将所选值分配给变量。的数量 变量必须与列数匹配。查询应该返回一个 单排。如果查询未返回任何行,则会显示带有错误代码的警告 发生1329(无数据),变量值保持不变。如果 查询返回多行,发生错误1172(结果包含 不止一行)。如果声明可能的话 检索多行,您可以使用LIMIT 1将结果集限制为 一行。
答案 1 :(得分:1)
您的列名和参数名称相同。重命名输入参数并将命令更改为:
SELECT CS_USER_TEAMS_ID INTO userteamid
FROM classicseasonmodel_classicseasonuserteam
WHERE user_id = @user_id;