Declaring variables creating a trigger in MySql error -
i'm having little problem creating trigger, because have declare few variables, , update operation using them. anyway, i'm gonna paste code, , error throws, , tell me i'm doing wrong. thanks.
create trigger trg_league_info after update on matchs begin declare hometotalmatchcount ,homewinmatchcount ,homedrawmatchcount ,homelossmatchcount ,homeleaguescore ,homeagainstgoal ,homeforgoal ,homeaverage int default 0; declare awaytotalmatchcount ,awaywinmatchcount ,awaydrawmatchcount ,awaylossmatchcount ,awayleaguescore ,awayagainstgoal ,awayforgoal ,awayaverage int default 0; declare hometeamid ,awayteamid ,homescore ,awayscore ,seasonid ,status int default 0; select @hometeamid=homeid, @awayteamid=awayid , @homescore=homescore , @awayscore=awayscore ,@seasonid=seasonid , @status=status inserted; if (@status == 2) select @hometotalmatchcount=leaguetotalmatchcount , @homewinmatchcount=leaguewincount , @homedrawmatchcount=leaguedrawcount , @homelossmatchcount=leaguelosscount , @homeagainstgoal=leagueagaintsgoalcount , @homeforgoal=leagueforgoalcount , @homeaverage= leagueavarage , @homeleaguescore = leaguescore team_info teamid == @hometeamid , seasonid==@seasonid; select @awaytotalmatchcount=leaguetotalmatchcount , @awaywinmatchcount=leaguewincount , @awaydrawmatchcount=leaguedrawcount , @awaylossmatchcount=leaguelosscount , @awayagainstgoal=leagueagaintsgoalcount , @awayforgoal=leagueforgoalcount , @awayaverage= leagueavarage , @awayleaguescore = leaguescore team_info teamid == @awayteamid , seasonid==@seasonid; if (@homescore>@awayscore) update team_info set leaguetotalmatchcount=@hometotalmatchcount+1 ,leaguewincount=@homewinmatchcount+1, leagueagaintsgoalcount=@homeagainstgoal+@awayscore , leagueforgoalcount=@homeforgoal+@homescore, leaguescore=@homeleaguescore+3 teamid == @hometeamid , seasonid==@seasonid; update team_info set leaguetotalmatchcount=@awaytotalmatchcount+1 ,leaguelosscount=@awaylossmatchcount+1, leagueagaintsgoalcount=@awayagainstgoal+@homescore , leagueforgoalcount=@awayforgoal+@awayscore, leaguescore=@awayleaguescore+1 teamid == @awayteamid , seasonid==@seasonid; elseif (@homescore==@awayscore) update team_info set leaguetotalmatchcount=@hometotalmatchcount+1 ,leaguedrawcount=@homedrawmatchcount+1, leagueagaintsgoalcount=@homeagainstgoal+@awayscore , leagueforgoalcount=@homeforgoal+@homescore, leaguescore=@homeleaguescore+2 teamid == @hometeamid , seasonid==@seasonid; update team_info set leaguetotalmatchcount=@awaytotalmatchcount+1 ,leaguedrawcount=@awaydrawmatchcount+1, leagueagaintsgoalcount=@awayagainstgoal+@homescore , leagueforgoalcount=@awayforgoal+@awayscore, leaguescore=@awayleaguescore+2 teamid == @awayteamid , seasonid==@seasonid; else update team_info set leaguetotalmatchcount=@hometotalmatchcount+1 ,leaguelosscount=@homelossmatchcount+1, leagueagaintsgoalcount=@homeagainstgoal+@awayscore , leagueforgoalcount=@homeforgoal+@homescore, leaguescore=@homeleaguescore+1 teamid == @hometeamid , seasonid==@seasonid; update team_info set leaguetotalmatchcount=@awaytotalmatchcount+1 ,leaguewincount=@awaywinmatchcount+1, leagueagaintsgoalcount=@awayagainstgoal+@homescore , leagueforgoalcount=@awayforgoal+@awayscore, leaguescore=@awayleaguescore+3 teamid == @awayteamid , seasonid==@seasonid; end if; end if; end
it throws following error:
1064 - have error in sql syntax; check manual corresponds mariadb server version right syntax use near 'begin declare hometotalmatchcount ,homewinmatchcount ,homedrawmatchcount ,h' @ line 3
last time wrote trigger did in mssql server , syntax little different. maybe i'm going need create procedure , call instead. if can help, i'd appreciate it.
you using wrong syntax trigger. check manual, must add for each row
construction. this:
create trigger trg_league_info after update on matchs each row begin ... end;
also, note declaring variables, not using them. hometotalmatchcount
, @hometotalmatchcount
different variables in mysql. hometotalmatchcount
variable local trigger , can accessed inside it. variables prefixed @
user variables, stored in session , can accessed everywhere inside current connection. persist after trigger executed. must either remove declarations , use user variables or remove @
variable names , use local declared variables.
Comments
Post a Comment