Jump to content

First Trigger Problems...


Jaguar83

Recommended Posts

Hi,

 

Thanks for taking the time to read my question. I'm writing a relatively simple (I hope!!!) trigger to update a field in a row in the table 'coinpack' (below) when the table 'pad' (below) gets a new row inserted. Basically I want the inserted pad.pad_70 to be added to the existing coinpack.coinpack_70 value (when coinpack.coinpack_id=pad.coinpack_id). I have no initial value in for coinpack.coinpack_70 (value = NULL?) so this may be the problem. The tables are:

 

coinpack:

+------------------+--------------+------+-----+---------+----------------+
| Field            | Type         | Null | Key | Default | Extra          |
+------------------+--------------+------+-----+---------+----------------+
| coinpack_id      | int(10)      | NO   | PRI | NULL    | auto_increment | 
| order_id         | int(11)      | NO   | MUL | NULL    |                | 
| coin_cdn         | varchar(15)  | NO   | MUL | NULL    |                | 
| coinpack_qty     | int(11)      | NO   |     | NULL    |                | 
| coinpack_service | varchar(30)  | YES  | MUL | NULL    |                | 
| coinpack_comm    | varchar(250) | YES  |     | NULL    |                | 
| coinpack_70      | int(11)      | YES  |     | NULL    |                | 
| coinpack_69      | int(11)      | YES  |     | NULL    |                | 
| coinpack_68      | int(11)      | YES  |     | NULL    |                | 
| coinpack_67      | int(11)      | YES  |     | NULL    |                | 
| coinpack_66      | int(11)      | YES  |     | NULL    |                | 
| coinpack_65      | int(11)      | YES  |     | NULL    |                | 
| coinpack_64      | int(11)      | YES  |     | NULL    |                | 
| coinpack_63      | int(11)      | YES  |     | NULL    |                | 
| coinpack_rejects | int(11)      | YES  |     | NULL    |                | 
+------------------+--------------+------+-----+---------+----------------+

 

pad:

+-------------+-------------+------+-----+---------+----------------+
| Field       | Type        | Null | Key | Default | Extra          |
+-------------+-------------+------+-----+---------+----------------+
| pad_id      | int(11)     | NO   | PRI | NULL    | auto_increment | 
| coinpack_id | int(11)     | NO   | MUL | NULL    |                | 
| pad_qty     | int(11)     | YES  |     | NULL    |                | 
| pad_70      | int(11)     | YES  |     | NULL    |                | 
| pad_69      | int(11)     | YES  |     | NULL    |                | 
| pad_68      | int(11)     | YES  |     | NULL    |                | 
| pad_67      | int(11)     | YES  |     | NULL    |                | 
| pad_66      | int(11)     | YES  |     | NULL    |                | 
| pad_65      | int(11)     | YES  |     | NULL    |                | 
| pad_64      | int(11)     | YES  |     | NULL    |                | 
| pad_63      | int(11)     | YES  |     | NULL    |                | 
| pad_rejects | int(11)     | YES  |     | NULL    |                | 
| pad_grader  | int(11)     | YES  | MUL | NULL    |                | 
| pad_edition | varchar(20) | YES  |     | NULL    |                | 
+-------------+-------------+------+-----+---------+----------------+

 

My trigger below seems a bit long winded...I went with the 'add everything to see if it works' approach.

Trigger:

CREATE TRIGGER update_grades 
BEFORE INSERT ON pad 
FOR EACH ROW 
BEGIN 
DECLARE old_70 INTEGER;

SELECT coinpack_70 
INTO old_70 
FROM coinpack 
WHERE coinpack.coinpack_id=NEW.pad_70;

IF old_70 = "" THEN
	IF NEW.pad_70 > 0 THEN 
		UPDATE coinpack 
		SET coinpack_70=NEW.pad_70 
		WHERE coinpack.coinpack_id=NEW.coinpack_id;
	END IF;
ELSE 
	IF NEW.pad_70 > 0 THEN 
		UPDATE coinpack 
		SET coinpack_70=old_70+NEW.pad_70 
		WHERE coinpack.coinpack_id=NEW.coinpack_id;
	END IF;
END IF;
END

 

MySQL accepts the trigger fine but when I insert a new row into pad, coinpack.coinpack_70 does not get updated (continues read NULL). Can anyone spot my error?

 

Thanks!

 

Link to comment
https://forums.phpfreaks.com/topic/191120-first-trigger-problems/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.