Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

not allowed to return a resultset from a trigger mysql

Tags:

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$$

delimiter ;

like image 748
Tarika Avatar asked Sep 18 '12 09:09

Tarika


People also ask

How do you call a stored procedure from a trigger in MySQL?

MySQL allows you to call a stored procedure from a trigger by using the CALL statement. By doing this, you can reuse the same stored procedure in several triggers. However, the trigger cannot call a stored procedure that has OUT or INOUT parameters or a stored procedure that uses dynamic SQL.

How do I delete an existing trigger in MySQL?

To destroy the trigger, use a DROP TRIGGER statement. You must specify the schema name if the trigger is not in the default schema: mysql> DROP TRIGGER test.

What is delimiter in MySQL trigger?

As we know that in MySQL we use the delimiter semicolon (;) to end each statement. The semicolon is the by default delimiter in MySQL. We need to change the delimiter, while creating a trigger, to tell MySQL that this is not the end of our trigger statement because we can use multiple statements in the trigger.

What are triggers in MySQL?

A trigger is a named database object that is associated with a table, and that activates when a particular event occurs for the table. Some uses for triggers are to perform checks of values to be inserted into a table or to perform calculations on values involved in an update.


1 Answers

You cannot execute SELECT statements from trigger. If you want to set variables, then use SELECT INTO statement, e.g. -

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
...

Also, rename variables, they have to differ from from field names.

like image 65
Devart Avatar answered Oct 02 '22 06:10

Devart