Jump to content

[SOLVED] help with a trigger


Darkmatter5

Recommended Posts

I have 2 tables

 

lvl2prod

levelproduction

02

15

 

plots

plot_idlevelprodvalue

102

215

 

Now I'm wanting to create a trigger for the prodvalue in plots.  I want it to get the production value from lvl2prod and update that value into prodvalue when level in plots is updated.

 

Can you have it pull data from another table to use as what to update a field to?

Link to comment
Share on other sites

I've got this so far, but it's not working.

 

CREATE TRIGGER calc_prodvalue
AFTER UPDATE ON plots
FOR EACH ROW
BEGIN
DECLARE pv INTEGER;

SELECT l.production
INTO pv
FROM lvls2prod l, plots p
WHERE p.level=l.level;

SET p.prodvalue=pv;
END;

 

I get this error, "#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 5 ".

Link to comment
Share on other sites

Okay I've come up with this

 

DELIMITER |

CREATE TRIGGER calc_prodvalue
AFTER UPDATE ON plots
FOR EACH ROW
BEGIN
DECLARE pv INTEGER;

SELECT l.production
INTO pv
FROM lvls2prod l, plots p
WHERE p.level=l.level;

UPDATE plots
SET NEW.prodvalue=pv;
END;
|

DELIMITER ;

 

Now I get this error: "#1235 - This version of MySQL doesn't yet support 'multiple triggers with the same action time and event for one table' "

Link to comment
Share on other sites

I ran it through phpmyadmin.  I found a post online that has some interesting info:

 

http://crazytoon.com/2008/03/03/mysql-error-1442-hy000-cant-update-table-t1-in-stored-functiontrigger-because-it-is-already-used-by-statement-which-invoked-this-stored-functiontrigger/

 

Here's the new code:

DELIMITER |

CREATE TRIGGER calc_prodvalue
BEFORE UPDATE ON plots
FOR EACH ROW

BEGIN
DECLARE pv INTEGER;

SELECT l.production
INTO pv
FROM lvls2prod l, plots p
WHERE p.level=l.level
AND p.plot_id=NEW.plot_id;

SET NEW.prodvalue=pv;
END;
|

DELIMITER ;

 

The problem is having to do this it doesn't update the value to the NEW value, but the OLD value before the value changes.  So the prodvalue is always old, not the new/current value based on level.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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