以下存储过程将返回包含正确总金额的列NoOfArticles
的结果。
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_membersarea`(IN reqdTable varchar(30), IN ScannerID INT, IN MemberID INT)
BEGIN
declare NoOfArticles INT(11);
SET @sql_text = concat("SELECT COUNT(*) AS NoOfArticles FROM t_annualdetails WHERE ScannerID = ", ScannerID);
PREPARE stmt FROM @sql_text;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END //
DELIMITER ;
那么远那么好的。这是我的问题。我希望能够使用这个'结果'在同一存储过程中的另一个查询中,但我不知道如何实现它。我希望它能像这样工作:
SET @NoOfArticles = NoOfArticles;
SET @sql_text = concat("UPDATE ", reqdTable, " SET Scan = ", @NoOfArticles, " WHERE MemberID = ", MemberID);
PREPARE stmt FROM @sql_text;
EXECUTE stmt;
显然,这不起作用,但我希望结果' (NoOfArticles
)放置在变量(即@NoOfArticles
)中,然后在@NoOfArticles
语句中使用该变量(UPDATE
)。
我整天都没有成功。我很感激一些指导。感谢。
答案 0 :(得分:0)
它是INTO
,而不是AS
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_membersarea`(IN reqdTable varchar(30), IN ScannerID INT, IN MemberID INT)
BEGIN
declare NoOfArticles INT(11);
/*----------------------------------here-v--into-instead-of-as-----------------*/
SET @sql_text = concat("SELECT COUNT(*) INTO NoOfArticles FROM t_annualdetails WHERE ScannerID = ", ScannerID);
PREPARE stmt FROM @sql_text;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET @NoOfArticles = NoOfArticles;
SET @sql_text = concat("UPDATE ", reqdTable, " SET Scan = ", @NoOfArticles, " WHERE MemberID = ", MemberID);
PREPARE stmt FROM @sql_text;
EXECUTE stmt;
/*you can also use parameters, but not for tablename*/
SET @sql_text = concat("UPDATE ", reqdTable, " SET Scan = ? WHERE MemberID = ?");
PREPARE stmt FROM @sql_text;
EXECUTE stmt USING NoOfArticles, MemberID;
END //
DELIMITER ;
修改强>
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_membersarea`(IN reqdTable varchar(30), IN ScannerID INT, IN MemberID INT)
BEGIN
SET @sql_text = concat("SELECT COUNT(*) INTO @NoOfArticles FROM t_annualdetails WHERE ScannerID = ", ScannerID);
PREPARE stmt FROM @sql_text;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
/*you can also use parameters, but not for tablename*/
SET @sql_text = concat("UPDATE ", reqdTable, " SET Scan = ? WHERE MemberID = ?");
PREPARE stmt FROM @sql_text;
EXECUTE stmt USING @NoOfArticles, MemberID;
END //
DELIMITER ;