0

我正在测试我的触发器,它工作得很好,除了我的变量似乎没有像他们应该的那样工作。

例如

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;

    SET the_last_inserted_id = LAST_INSERT_ID();
    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)  select cs_id,@the_last_insert_id from class_students where cs_class_id = 1 AND cs_year_id = 1;

END //
DELIMITER ;

在这一行

insert into examination_data (ed_cs_id,ed_examination_id)  select cs_id,the_last_insert_id from class_students where cs_class_id = 1 AND cs_year_id = 1;

the_last_insert_id被视为一个专栏

当我尝试这个时@the_last_insert_id,它没有被视为一个列,但它也不起作用。我还没有尝试过我的其他变量。

例如,我将如何定义和使用它the_last_inserted_id = LAST_INSERT_ID();

4

1 回答 1

0

你可以设置

Select STH into the_last_inserted_id from TBL limit 1;

insert into examination_data (ed_cs_id,ed_examination_id)
select cs_id,the_last_insert_id 
from class_students
where cs_class_id = 1 AND cs_year_id = 1;

编辑:@ - 您在没有声明的情况下使用,只是设置

Select STH into @new_id from TBL limit 1;

进而

insert into examination_data (ed_cs_id,ed_examination_id)
select cs_id,@the_last_insert_id
from class_students
where cs_class_id = 1 AND cs_year_id = 1;
于 2013-07-22T13:41:03.323 回答