不允许从触发器mysql返回结果集

时间:2012-09-18 09:49:27

标签: mysql

delimiter $$
CREATE TRIGGER REDUCE_NOTE_COUNT
 AFTER DELETE ON iv_notes
 FOR EACH ROW  BEGIN
DECLARE supplierid int(11);
DECLARE customerid int(11);

SELECT supplierid ,customerid FROM iv_documents WHERE id=OLD.note_documentid;
SET supplierid=supplierid;
SET customerid=customerid;

IF supplierid=OLD.note_companyid THEN
    update iv_documents 
            set supplier_notes=supplier_notes-1 
            where id=OLD.note_documentid and supplier_notes>0;
END IF;
IF customerid=OLD.note_companyid THEN
    update iv_documents set customer_notes=customer_notes-1 
            where id=OLD.note_documentid 
            and customer_notes>0 ;
END IF;
END$$

分隔符;

1 个答案:

答案 0 :(得分:22)

您无法从触发器执行SELECT语句。如果要设置变量,请使用SELECT INTO语句,例如 -

DECLARE supplierid_ INT(11);
DECLARE customerid_ INT(11);

SELECT
  supplierid, customerid
INTO
  supplierid_, customerid_
FROM
  iv_documents
WHERE
  id = OLD.note_documentid;

IF supplierid_ = OLD.note_companyid THEN
...

此外,重命名变量,它们必须与字段名称不同。