场景:有一张表test,有两个字段,id varchar(100),count int(10),我的想法是在对这个表插入的插入一条记录的时候,能对count进行自动加,比如insert into test("abc",1),执行之后的结果是"abc",2。做法:利用触发器的new可以实现这个,但是我想利用游标进行来实现,不知道mysql的游标是不是可以跟oracle的游标一样具有这个功能,我初步的写一个,但是给我报错了,错误信息如下:ERROR 1442 (HY000): Can't update table 'test' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.希望高手可以指点一下,如果能的话,最好帮我写段代码,谢谢!本人出来乍到,没有多少分,希望大家能帮个忙!~谢谢!!

解决方案 »

  1.   

     (不要高估你的汉语表达能力或者我的汉语理解能力)
       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  2.   

    贴出你的SHOW CREATE TABLE,
    贴出你的测试代码。你的游标用在触发器里面吗??
      

  3.   

    像上面的两位表示谢意,第一次发帖子,没弄清楚,我再补一下:
    表结构: 数据库是mysql
    create table tbl_cursor
    (
    uuid varchar(100),
    num int(10)
    );
    触发器:
    create trigger tbl_cursor_au
    after insert 
    on tbl_cursor
    for each rowbegin
      declare temp int(10) default 0;
      declare my_cur cursor for select * from tbl_cursor where uuid = new.uuid for update;

    open my_cur;
        set temp = new.num + 1;
    update tbl_cursor set num = temp where uuid = new.uuid;
    close my_cur;
    end;
    插入语句:
    insert into tbl_cursor values(uuid(),12);
    报错:ERROR 1442 (HY000): Can't update table 'tbl_cursor' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.我已经声明游标的类型是for update了,为什么还不能更改?
      

  4.   

    MYSQL在当前表的触发器中不可以对该表进行insert / update 等操作,以防止递归触发。
      

  5.   

    那像这样的情况,如果一定要实现相应的功能,是不是可以通过把需要修改update的属性和主键拉出来做个表,进行相应的操作啊?