2012-09-18 72 views
6
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$$ 

分隔符;不允許從觸發器返回結果集mysql

+2

新用戶提示:請問,人們,問問題,這是不明顯的你要求。包括您使用的版本,並可能包含錯誤消息:) –

回答

20

您不能從觸發器執行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 
... 

另外,重命名變量,它們必須與字段名稱不同。

+0

謝謝you.it作品 – Tarika

+0

如果確定,請接受答案。 – Devart

相關問題