Jump to content

[SOLVED] Trigger Syntax


josborne

Recommended Posts

I think I just need a new set of eyes on this.

 

I have never used triggers before but need one now. From what I can tell, I have the syntax right and the trigger should work but since it is giving me a syntax error when I try to create it, I am obviously wrong.

 

CREATE TRIGGER Val_Update_TR AFTER INSERT ON Unit_tbl
FOR EACH ROW
BEGIN
UPDATE Pred_tbl SET Val_Against = (

SELECT SUM( Unit_For ) / SUM( Unit_Against )
FROM Unit_tbl
WHERE Pred_ID = NEW.Pred_ID
)
WHERE Pred_ID = NEW.Pred_ID;
END;

 

The SELECT runs fine on its own (SELECT SUM( Unit_For ) / SUM(Unit_Against ) FROM Unit_tbl WHERE Pred_ID = NEW.Pred_ID) so it must be with the trigger code.

 

Help?

Link to comment
Share on other sites

Maybe if you store the select into a variable and use the variable to the update query..

 

Something like this..

 

SET @resultset = (SELECT SUM( Unit_For ) / SUM( Unit_Against )
FROM Unit_tbl
WHERE Pred_ID = NEW.Pred_ID);

UPDATE Pred_tbl SET Val_Against = @resultset WHERE Pred_ID = NEW.Pred_ID;

Link to comment
Share on other sites

Maybe I am confused about what you are getting at but I tried:

 

CREATE TRIGGER Val_Update_TR AFTER INSERT ON Unit_tbl
FOR EACH ROW
BEGIN
SET @resultset = (SELECT SUM( Unit_For ) / SUM( Unit_Against )
FROM Unit_tbl
WHERE Pred_ID = NEW.Pred_ID);

UPDATE Pred_tbl SET Val_Against = @resultset WHERE Pred_ID = NEW.Pred_ID;

 

This produces a syntax error as well.

Link to comment
Share on other sites

I dod not originall change the delimiter. I just tried it changing the delimiter to | but that didn't help.

 

I have looked at this thing a hundred times and I cannot figure out what it wrong. I have compared it to countless examples of triggers I have come across and can't find the problem.

Link to comment
Share on other sites

Try  this..

 

DELIMITER $$

USE `db`$$

DROP TRIGGER /*!50032 IF EXISTS */ `tai_1`$$

CREATE
    TRIGGER `ta1_1` AFTER INSERT ON `tablename` 
    FOR EACH ROW BEGIN
        SET @resultset = (SELECT SUM( Unit_For ) / SUM( Unit_Against )
        FROM Unit_tbl
        WHERE Pred_ID = NEW.Pred_ID);

        UPDATE Pred_tbl SET Val_Against = @resultset WHERE Pred_ID = NEW.Pred_ID;
    END;
$$

DELIMITER ;

Link to comment
Share on other sites

gassaz, I believe that worked. No syntax error but now it appears I have a permissions issue (must be Super to create triggers). I am tracking down why I don't have permissions now. Hopefully, when I get that fixed I will be on my way.

 

I'll update as soon as I know what is going on.

 

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.