错误:子查询返回多行

时间:2014-10-15 12:19:32

标签: java mysql jdbc callable-statement

您好我一直尝试通过CallableStatement调用过程来选择多行。当我尝试将结果集填充到组合框时,代码将返回错误,如下所示。

Java错误:

                   java.sql.SQLException: Subquery returns more than 1 row

存储过程:

CREATE DEFINER=`user_name`@`%` PROCEDURE `GET_USER_PROFILE`(
            IN p_user_id VARCHAR(150),
            IN p_role VARCHAR(150),
            OUT p_user_data VARCHAR(200),
            OUT p_city VARCHAR(150),
            OUT p_state VARCHAR(150),
            OUT p_country VARCHAR(150),
            OUT q_Msg VARCHAR(150))
BEGIN

DECLARE available INT DEFAULT 0;
 SET p_city = (SELECT CITY FROM countries GROUP BY CITY);
 SET p_state = (SELECT STATE FROM countries GROUP BY STATE);
 SET p_country = (SELECT COUNTRY FROM countries GROUP BY COUNTRY);

SELECT COUNT(EMAIL) INTO available FROM STAFF_PROFILE WHERE EMAIL = p_user_id AND ROLE = p_role;

IF(available=1) THEN
        SET p_user_data = (SELECT * FROM STAFF_PROFILE WHERE EMAIL = p_user_id AND ROLE = p_role );
else
    SET q_Msg = 'USER_LOGGED_FIRST';
END IF;

END

1 个答案:

答案 0 :(得分:1)

@DaveHowes和@Ilya是正确的,问题在于您的SQL语句。

让我们在您的国家/地区表中包含以下内容:

city            state           country
'New York'      'New York'      'USA'
'Los Angeles'   'California'    'USA'
'Chicago'       'Illinois'      'USA'
'Ottawa'        ''              'Canada'

现在,如果我们从您的示例中获取子查询:

SELECT city FROM countries GROUP BY city

将返回:

city
'New York'
'Los Angeles'
'Chicago'
'Ottawa'

您正在尝试将多个结果分配给varchar,因此您将获得异常“子查询返回多于一行”。