我需要执行全表级扫描,因为应该根据用户ID检索多行。
因为我曾经使用sqlMessage声明“子查询返回多于1行”。
{ 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;
我需要如何检索数组形式的所有行并将其存储到变量中,或者我应该如何编码以解决错误消息? < / strong>
答案 0 :(得分:1)
使用视图存储您获取的数据而不是变量
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 ;