将值传递到此存储过程的正确方法是什么,显然我没有以正确的方式执行,并且在一个过程中执行两个语句是安全的还是我应该分别创建两个语句?
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `drop_student`(IN section_id VARCHAR(20), IN student_id VARCHAR(20))
BEGIN
SET @section_id=section_id;
SET @student_id=student_id;
PREPARE STMT2 FROM
"DELETE FROM transcript
WHERE STUDENT_ID = @student_id
AND SECTION_ID = @section_id ";
PREPARE STMT FROM
"DELETE FROM course
WHERE STUDENT_ID = @student_id
AND SECTION_ID = @section_id ";
EXECUTE STMT2 USING @section_id,@student_id;
EXECUTE STMT USING @section_id,@student_id;
END
答案 0 :(得分:3)
您确定在上面的代码中使用SQL Server
(我看到sql server
标记)吗?我想这是MySQL
。这样做
PREPARE STMT2 FROM "DELETE FROM transcript WHERE STUDENT_ID = ? AND
SECTION_ID = ?";
EXECUTE STMT2 USING @student_id, @section_id;
DEALLOCATE PREPARE STMT2;
-- same as STMT
答案 1 :(得分:1)
要调用您要执行的过程:
CALL drop_student('foo','bar');
答案 2 :(得分:0)
你试过这个吗?
EXEC STMT2 @section_id @student_id;
EXEC STMT @section_id @student_id;
答案 3 :(得分:0)
您不需要在此处使用预准备语句。实际上,您甚至可以将两个DELETE
语句组合成一个语句:
CREATE PROCEDURE drop_student (
IN p_section_id VARCHAR(20),
IN p_student_id VARCHAR(20)
)
DELETE transcript, course
FROM transcript JOIN course USING (STUDENT_ID, SECTION_ID)
WHERE STUDENT_ID = p_student_id AND SECTION_ID = p_section_id;
此外,您似乎可能尝试手动实施foreign key constraints使用ON DELETE CASCADE
选项自动提供的行为。