If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Cursor in trigger

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-18-11, 06:21
ssm2010 ssm2010 is offline
Registered User
 
Join Date: Jun 2011
Posts: 1
Cursor in trigger

Hi,

I am having difficulty using triggers in MySQL Workbench. I have defined the following code under the triggers tab (see below). I thought that I could use a cursor to walk over the source table that I created using SELECT, which joins two tables, however the code below updates the wrong row of the target table (matchRecord), and updates it using a summation applied to the entire source table. To make it clearer, the result of the SELECT action is:

matches, teams, goalsConceded
1, 1, 1
1, 2, 1
2, 1, 1
2, 2, 1 (goal inserted)

Inserting a goal into match 2 should update the matchRecord to produce:

match, homeTeam, homeTeamScore, awayTeam, awayTeamScore
1, 1, 1, 2, 1
2, 1, 1, 2, 1

Instead I get this:

match, homeTeam, homeTeamScore, awayTeam, awayTeamScore
1, 1, 1, 2, 4
2, 1, 0, 2, 1

In otherwords it takes only the first row from the source table, then repeatedly adds this single row to first row in the target table, when I would expect it to update the row in the target table corresponding to the inserted row in the source table.

I am a struggling a bit and I've tried every forum I can find.

Thanks for any help,

Andy.

-- Trigger DDL Statements
DELIMITER $$
CREATE TRIGGER project.trigger AFTER INSERT ON goalsRecord

FOR EACH ROW

BEGIN

DECLARE done INT DEFAULT 0;
DECLARE a, matches INT;
DECLARE b, teams INT;
DECLARE c, goalsConceded INT;

DECLARE cursor1 CURSOR FOR
SELECT matchRecord_matchID AS matches, teamRecord_teamID AS teams, COUNT(minutesOfGoal) AS goalsConceded
FROM goalsRecord LEFT JOIN matchTeamPlayerSelection
ON matchTeamPlayerSelection_playerRecord_concedingPla yerID = playerRecord_playerID
WHERE matchRecord_matchID = matchTeamPlayerSelection_matchRecord_matchID
GROUP BY teams, matches
ORDER BY matches, teams;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

UPDATE matchRecord
SET matchRecord.hometeamScore = DEFAULT
WHERE matchID != NULL;

UPDATE matchRecord
SET matchRecord.awayTeamScore = DEFAULT
WHERE matchID != NULL;

OPEN cursor1;

read_loop: LOOP

FETCH cursor1 INTO a,b,c;

IF done THEN
LEAVE read_loop;
END IF;

UPDATE matchRecord
SET matchRecord.homeTeamScore = c
WHERE matchRecord.matchID = a AND matchRecord.teamRecord_awayTeamID = b;

UPDATE matchRecord
SET matchRecord.awayTeamScore = c
WHERE matchRecord.matchID = a AND matchRecord.teamRecord_homeTeamID = b;

END LOOP;

CLOSE cursor1;

END;

$$
Reply With Quote
  #2 (permalink)  
Old 06-19-11, 05:49
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
I have looked at this and I think the issue might be with your left join in the cursor definition. Do you really need this? It is not clear from your explanation above why this should be in there. Also when comparing again NULL values you must use matchID IS NULL or matchID IS NOT NULL. In your code above both will not match anything and nothing will get updated.
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On