在存储过程中检索多行
Retrieving more than one rows in a stored procedure
我需要执行完整的 table 级别扫描,因为它应该根据用户 ID 检索多行。
因为我曾经得到 sqlMessage 说明 'Subquery returns more than 1 row'。
{ Error: ER_SUBQUERY_NO_1_ROW: Subquery returns more than 1 row
...
code: 'ER_SUBQUERY_NO_1_ROW',
errno: 1242,
sqlMessage: 'Subquery returns more than 1 row',
sqlState: '21000',
index: 0,
sql: 'CALL GetLocationTag(1)' }
这是对 GetLocationTag()
代码的调用
DELIMITER $$
CREATE
DEFINER = `root`@`localhost`
PROCEDURE `someDB`.`GetLocationTag`(IN `UserId` BIGINT)
LANGUAGE SQL
DETERMINISTIC
READS SQL DATA
SQL SECURITY DEFINER
COMMENT 'Retrieves Multiple Stored Location Tags, Address, Location, etc. based on User_ID'
BEGIN
DECLARE flag INT(11);
DECLARE locRecord VARCHAR(255);
IF EXISTS (SELECT * FROM `location_map` WHERE `user_id` = User_Id ) THEN
BEGIN
SELECT * INTO locRecord FROM `location_map` WHERE `user_id` = User_Id;
SET flag = 1;
END;
ELSE
SET flag = 0;
SET locRecord = 0;
END IF;
SELECT locRecord, flag;
END$$
DELIMITER ;
对于下面的查询,
SELECT * INTO locRecord FROM `location_map` WHERE `user_id` = User_Id;
我如何需要以数组的形式检索所有行并将其存储到变量中或者我应该如何编写代码以便解决错误消息?
使用 视图 来存储您获取的数据而不是变量
DELIMITER $$
CREATE
DEFINER = `root`@`localhost`
PROCEDURE `someDB`.`GetLocationTag`(IN `User_Id` BIGINT)
LANGUAGE SQL
DETERMINISTIC
MODIFIES SQL DATA
SQL SECURITY DEFINER
COMMENT 'Retrieves Multiple Stored Location Tags, Address, Location, etc. based on User_ID'
BEGIN
DECLARE flag INT(11);
DECLARE locRecord VARCHAR(255);
IF EXISTS (SELECT * FROM `location_map` WHERE `user_id` = User_Id ) THEN
BEGIN
SET @viewQry = CONCAT('CREATE OR REPLACE VIEW `someDB`.`temp` as SELECT *, 1 as flag FROM `location_map` WHERE `user_id` = ',User_Id,';');
PREPARE stmt FROM @viewQry;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT * FROM temp;
END;
ELSE
SELECT 0 AS locRecord, 0 AS flag;
END IF;
END$$
DELIMITER ;
我需要执行完整的 table 级别扫描,因为它应该根据用户 ID 检索多行。
因为我曾经得到 sqlMessage 说明 'Subquery returns more than 1 row'。
{ Error: ER_SUBQUERY_NO_1_ROW: Subquery returns more than 1 row
...
code: 'ER_SUBQUERY_NO_1_ROW',
errno: 1242,
sqlMessage: 'Subquery returns more than 1 row',
sqlState: '21000',
index: 0,
sql: 'CALL GetLocationTag(1)' }
这是对 GetLocationTag()
代码的调用
DELIMITER $$
CREATE
DEFINER = `root`@`localhost`
PROCEDURE `someDB`.`GetLocationTag`(IN `UserId` BIGINT)
LANGUAGE SQL
DETERMINISTIC
READS SQL DATA
SQL SECURITY DEFINER
COMMENT 'Retrieves Multiple Stored Location Tags, Address, Location, etc. based on User_ID'
BEGIN
DECLARE flag INT(11);
DECLARE locRecord VARCHAR(255);
IF EXISTS (SELECT * FROM `location_map` WHERE `user_id` = User_Id ) THEN
BEGIN
SELECT * INTO locRecord FROM `location_map` WHERE `user_id` = User_Id;
SET flag = 1;
END;
ELSE
SET flag = 0;
SET locRecord = 0;
END IF;
SELECT locRecord, flag;
END$$
DELIMITER ;
对于下面的查询,
SELECT * INTO locRecord FROM `location_map` WHERE `user_id` = User_Id;
我如何需要以数组的形式检索所有行并将其存储到变量中或者我应该如何编写代码以便解决错误消息?
使用 视图 来存储您获取的数据而不是变量
DELIMITER $$
CREATE
DEFINER = `root`@`localhost`
PROCEDURE `someDB`.`GetLocationTag`(IN `User_Id` BIGINT)
LANGUAGE SQL
DETERMINISTIC
MODIFIES SQL DATA
SQL SECURITY DEFINER
COMMENT 'Retrieves Multiple Stored Location Tags, Address, Location, etc. based on User_ID'
BEGIN
DECLARE flag INT(11);
DECLARE locRecord VARCHAR(255);
IF EXISTS (SELECT * FROM `location_map` WHERE `user_id` = User_Id ) THEN
BEGIN
SET @viewQry = CONCAT('CREATE OR REPLACE VIEW `someDB`.`temp` as SELECT *, 1 as flag FROM `location_map` WHERE `user_id` = ',User_Id,';');
PREPARE stmt FROM @viewQry;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT * FROM temp;
END;
ELSE
SELECT 0 AS locRecord, 0 AS flag;
END IF;
END$$
DELIMITER ;