我想使用此程序选择我想要的任何随机值
delimiter $$
CREATE PROCEDURE randomdigit(troy INT)
BEGIN
select troy;
END$$
delimiter ;
要使用它,我打电话给call randomdigit(n);
但是,当我尝试将程序分配给触发器中的变量时,我会收到此错误
/ * SQL错误(1064):您的SQL语法有错误;检查 手册,对应右边的MySQL服务器版本 在'call randomdigit(1)附近使用的语法; SET the_class_id =(从第11行的e'中选择exam_class_id * /
这是我的触发器
DELIMITER //
CREATE TRIGGER lestrigger
AFTER INSERT ON examinations
FOR EACH ROW
BEGIN
DECLARE the_last_inserted_id INT ;
DECLARE the_class_id INT;
DECLARE the_year_id INT;
DECLARE lesrandom INT;
SET the_last_inserted_id = LAST_INSERT_ID();
SET lesrandom = call randomdigit(1);
SET the_class_id = (select examination_class_id from examinations where examination_id = 1);
SET the_year_id = (select examination_class_id from examinations where examination_id = 1);
insert into examination_data (ed_cs_id,ed_examination_id) VALUES (( select cs_id from class_students where cs_class_id = 1 AND cs_year_id = 1 ),lesrandom);
END //
DELIMITER ;
以这种方式将过程分配给变量是否正确?。
答案 0 :(得分:3)
Akhil的答案是一个可能的解决方案。如果您需要存储过程,则必须使用OUT
参数。
delimiter $$
CREATE PROCEDURE randomdigit(IN troy INT, OUT result INT)
BEGIN
set result = troy;
END$$
delimiter ;
并将其称为:
DELIMITER //
CREATE TRIGGER lestrigger
AFTER INSERT ON examinations
FOR EACH ROW
BEGIN
DECLARE the_last_inserted_id INT ;
DECLARE the_class_id INT;
DECLARE the_year_id INT;
DECLARE lesrandom INT;
SET the_last_inserted_id = LAST_INSERT_ID();
call randomdigit(1, lesrandom);
SET the_class_id = (select examination_class_id from examinations where examination_id = 1);
SET the_year_id = (select examination_class_id from examinations where examination_id = 1);
insert into examination_data (ed_cs_id,ed_examination_id) VALUES (( select cs_id from class_students where cs_class_id = 1 AND cs_year_id = 1 ),lesrandom);
END //
DELIMITER ;
答案 1 :(得分:1)
将其更改为功能
delimiter $$
CREATE function randomdigit(troy INT) returns int
BEGIN
return troy;
END$$
delimiter ;
按如下方式更改触发器
DELIMITER //
CREATE TRIGGER lestrigger
AFTER INSERT ON examinations
FOR EACH ROW
BEGIN
DECLARE the_last_inserted_id INT ;
DECLARE the_class_id INT;
DECLARE the_year_id INT;
DECLARE lesrandom INT;
SET the_last_inserted_id = LAST_INSERT_ID();
SET lesrandom = randomdigit(1);
SET the_class_id = (select examination_class_id from examinations where examination_id = 1);
SET the_year_id = (select examination_class_id from examinations where examination_id = 1);
insert into examination_data (ed_cs_id,ed_examination_id) VALUES (( select cs_id from class_students where cs_class_id = 1 AND cs_year_id = 1 ),lesrandom);
END //
DELIMITER ;
希望这很好