创建触发器时出现问题

发布于 2024-08-31 00:50:32 字数 1331 浏览 3 评论 0原文

我在创建触发器时遇到问题,我不确定为什么会出错。您能帮助我吗?

我写了一个下面提到的触发器,它在尝试执行触发器时给了我这个错误......

1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在 'DECLARE store_itemid INTEGER; 附近使用的正确语法。 store_hostid 整数; store_desc VARCHAR(255);'在第 4 行

代码:

mysql> delimiter //
   CREATE TRIGGER init_trigger 
   AFTER INSERT ON test_trigger
   FOR EACH ROW
   DECLARE 
     store_itemid INT;
     store_hostid INT;
     store_desc VARCHAR(255);
     store_key VARCHAR(255);
     store_lastvalue VARCHAR(255);
     store_lastclock VARCHAR(255);
     store_preval INT;
     store_status INT;
   BEGIN

     SELECT itemid into store_itemid,
            hostid into store_hostid,
            description into store_desc,
            key_ into store_key,
            lastvalue into store_lastvalue,
            lastclock into store_lastclock,
            prevvalue into store_preval,
            status into store_status 
       FROM test_triggers 
      WHERE lastvalue > 80;

     IF store_lastvalue > 80 THEN
        INSERT INTO test_triggers1 
          (itemid,hostid,description,key,lastvalue,lastclock,prevvalue,status) 
        VALUES
          (store_itemid,store_hostid,store_desc,store_key,store_lastvalue,store_lastclock,store_preval,store_status);

     END IF;
   END;
   // 

I am facing a problem in creating a trigger and i am not sure why this is erroring out.can you please help me.

I have written a trigger mentioned below and it gives me this error upon trying to execute the trigger ...

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 'DECLARE store_itemid INTEGER; store_hostid INTEGER; store_desc VARCHAR(255);' at line 4

Code:

mysql> delimiter //
   CREATE TRIGGER init_trigger 
   AFTER INSERT ON test_trigger
   FOR EACH ROW
   DECLARE 
     store_itemid INT;
     store_hostid INT;
     store_desc VARCHAR(255);
     store_key VARCHAR(255);
     store_lastvalue VARCHAR(255);
     store_lastclock VARCHAR(255);
     store_preval INT;
     store_status INT;
   BEGIN

     SELECT itemid into store_itemid,
            hostid into store_hostid,
            description into store_desc,
            key_ into store_key,
            lastvalue into store_lastvalue,
            lastclock into store_lastclock,
            prevvalue into store_preval,
            status into store_status 
       FROM test_triggers 
      WHERE lastvalue > 80;

     IF store_lastvalue > 80 THEN
        INSERT INTO test_triggers1 
          (itemid,hostid,description,key,lastvalue,lastclock,prevvalue,status) 
        VALUES
          (store_itemid,store_hostid,store_desc,store_key,store_lastvalue,store_lastclock,store_preval,store_status);

     END IF;
   END;
   // 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

似梦非梦 2024-09-07 00:50:32

你必须在“ROW”之后添加“BEGIN”

U must add "BEGIN" after "ROW"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文