声明在MySql错误中创建触发器的变量

时间:2016-06-11 04:31:48

标签: mysql variables triggers declaration

我在创建触发器时遇到一些问题,因为我必须声明一些变量,然后使用它们进行一些更新操作。无论如何,我要粘贴代码,以及它抛出的错误,希望有人会告诉我我做错了什么。感谢。

    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 FROM inserted;

       IF (@Status == 2) THEN
          SELECT @HomeTotalMatchCount=LeagueTotalMatchCount , @HomeWinMatchCount=LeagueWinCount , @HomeDrawMatchCount=LeagueDrawCount , @HomeLossMatchCount=LeagueLossCount  , @HomeAgainstGoal=LeagueAgaintsGoalCount , @HomeForGoal=LeagueForGoalCount , @HomeAverage=    LeagueAvarage , @HomeLeagueScore = LeagueScore FROM team_info WHERE TeamID == @HomeTeamID AND SeasonID==@SeasonID;
          SELECT @AwayTotalMatchCount=LeagueTotalMatchCount , @AwayWinMatchCount=LeagueWinCount , @AwayDrawMatchCount=LeagueDrawCount , @AwayLossMatchCount=LeagueLossCount  , @AwayAgainstGoal=LeagueAgaintsGoalCount , @AwayForGoal=LeagueForGoalCount , @AwayAverage=    LeagueAvarage , @AwayLeagueScore = LeagueScore FROM team_info WHERE TeamID == @AwayTeamID AND SeasonID==@SeasonID;

          IF (@HomeScore>@AwayScore) THEN
             uPDATE team_info SET LeagueTotalMatchCount=@HomeTotalMatchCount+1 ,LeagueWinCount=@HomeWinMatchCount+1, LeagueAgaintsGoalCount=@HomeAgainstGoal+@AwayScore , LeagueForGoalCount=@HomeForGoal+@HomeScore, LeagueScore=@HomeLeagueScore+3 WHERE TeamID == @HomeTeamID AND SeasonID==@SeasonID;
             uPDATE team_info SET LeagueTotalMatchCount=@AwayTotalMatchCount+1 ,LeagueLossCount=@AwayLossMatchCount+1, LeagueAgaintsGoalCount=@AwayAgainstGoal+@HomeScore , LeagueForGoalCount=@AwayForGoal+@AwayScore, LeagueScore=@AwayLeagueScore+1 WHERE TeamID == @AwayTeamID AND SeasonID==@SeasonID;
          ELSEIF (@HomeScore==@AwayScore) THEN
             uPDATE team_info SET LeagueTotalMatchCount=@HomeTotalMatchCount+1 ,LeagueDrawCount=@HomeDrawMatchCount+1, LeagueAgaintsGoalCount=@HomeAgainstGoal+@AwayScore , LeagueForGoalCount=@HomeForGoal+@HomeScore, LeagueScore=@HomeLeagueScore+2 WHERE TeamID == @HomeTeamID AND SeasonID==@SeasonID;
             uPDATE team_info SET LeagueTotalMatchCount=@AwayTotalMatchCount+1 ,LeagueDrawCount=@AwayDrawMatchCount+1, LeagueAgaintsGoalCount=@AwayAgainstGoal+@HomeScore , LeagueForGoalCount=@AwayForGoal+@AwayScore, LeagueScore=@AwayLeagueScore+2 WHERE TeamID == @AwayTeamID AND SeasonID==@SeasonID;
          ELSE
             uPDATE team_info SET LeagueTotalMatchCount=@HomeTotalMatchCount+1 ,LeagueLossCount=@HomeLossMatchCount+1, LeagueAgaintsGoalCount=@HomeAgainstGoal+@AwayScore , LeagueForGoalCount=@HomeForGoal+@HomeScore, LeagueScore=@HomeLeagueScore+1 WHERE TeamID == @HomeTeamID AND SeasonID==@SeasonID;
             uPDATE team_info SET LeagueTotalMatchCount=@AwayTotalMatchCount+1 ,LeagueWinCount=@AwayWinMatchCount+1, LeagueAgaintsGoalCount=@AwayAgainstGoal+@HomeScore , LeagueForGoalCount=@AwayForGoal+@AwayScore, LeagueScore=@AwayLeagueScore+3 WHERE TeamID == @AwayTeamID AND SeasonID==@SeasonID;
          END IF;    
       END IF;  
    END

它会抛出以下错误:

1064 - 您的SQL语法出错;检查与MariaDB服务器版本对应的手册,以便在第3行的'BEGIN DECLARE HomeTotalMatchCount,HomeWinMatchCount,HomeDrawMatchCount,H'附近使用正确的语法

上次我写了一个触发器,我在MSSQL Server中做了它,语法有点不同。也许我只需要创建一个程序并调用它。如果有人可以提供帮助,我会很感激。

1 个答案:

答案 0 :(得分:0)

您使用错误的语法进行触发。检查manual,您必须添加UIAlertController * alert= [UIAlertController alertControllerWithTitle:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"] message:@"share via" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* fbButton = [UIAlertAction actionWithTitle:@"Facebook" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { // Add your code }]; UIAlertAction* twitterButton = [UIAlertAction actionWithTitle:@"Twitter" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { // Add your code }]; UIAlertAction* watsappButton = [UIAlertAction actionWithTitle:@"Whatsapp" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { // Add your code }]; UIAlertAction* emailButton = [UIAlertAction actionWithTitle:@"Email" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { // Add your code }]; UIAlertAction* cancelButton = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { //Handel no, thanks button }]; [alert addAction:fbButton]; [alert addAction:twitterButton]; [alert addAction:watsappButton]; [alert addAction:emailButton]; [alert addAction:cancelButton]; [self presentViewController:alert animated:YES completion:nil]; 构造。像这样:

FOR EACH ROW

另外,请注意您要声明变量,但不使用它们。 CREATE TRIGGER trg_league_info AFTER UPDATE ON matchs FOR EACH ROW BEGIN ... END; HomeTotalMatchCount是MySQL中的不同变量。您的@HomeTotalMatchCount变量是此触发器的本地变量,只能在其中访问。但是以HomeTotalMatchCount为前缀的变量是用户变量,它们存储在会话中,可以在当前连接中的任何位置访问。它们将在执行触发器后持续存在。您必须删除声明并使用用户变量或从变量名中删除@并使用本地声明的变量。