0

我想使用此过程选择我想要的任何随机值

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' 中选择考试类别 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 ;

以这种方式将过程分配给变量是否正确?

4

2 回答 2

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 ;
于 2013-07-22T11:06:01.733 回答
2

将其更改为函数

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 ;

希望这很好

于 2013-07-22T11:00:04.793 回答