介绍下这个过程希望实现的功能,表brandlist里放的是品牌名brandkey,表temp_no里放的是关键词uni_kw,想在表temp_no的每个uni_kw中循环查找品牌名,如果包含某个品牌名,即更新变量tempbrand,最后将这个变量update到表temp_no中此uni_kw对应的brand字段。brandlist表内的记录为100+,temp_no本来的数据量为500W,此过程运行了一个晚上还没有执行完,后来我将temp_no的数据量调整为3W,过程20分钟执行完了,但是Affected rows: 0 ,brand字段没有改变。过程的数据访问设置为modifies sql data,权限应该没有问题。不知道是程序逻辑有问题还是语句有问题,我是mySQL的新手,特来请教各位。begin
declare this_id  int;
declare this_kw varchar(100);
declare done int default 0;
declare c1 cursor for select uni_id,uni_kw from temp_no;
declare continue handler for not found set done=1;open c1;
c1:Loop
      if done=1 then
          leave c1;
      else
          fetch c1 into this_id,this_kw;
          begin
                  declare this_brand varchar(20);
                  declare tempbrand varchar(50);
                  declare done2 int default 0;
                  declare c2 cursor for select brandkey from brandlist;
                  declare continue handler for not found set done2=1;
                  open c2;
                  c2:loop
                           if done2=1 then
                                 leave c2;
                           else
                                 fetch c2 into this_brand;
                                 if instr(this_kw,this_brand)>0 then
                                      set tempbrand=concat(tempbrand,';',this_brand);
                                 end if;
                            end if;
                    end loop;
                   update temp_no set brand=tempbrand where uni_id=this_id;
            end;
       end if;
end loop;
end

解决方案 »

  1.   

    加select打印 慢慢调试吧
      

  2.   

    楼上确认是MYSQL的存储过程代码? MYSQL中DECLARE语句必须放在首部。而你的代码在中部也使用了
    begin
                      declare this_brand varchar(20);
                      declare tempbrand varchar(50);楼主用的是什么版本?
      

  3.   

    谢谢各位的回复,这个问题已解决
    有3个问题:
    1.concat字符连接函数,参数如果有null,则结果为null。而我的变量默认值为null,所以结果都为null没变。
    2.update前没有对变量进行判断,导致更新操作频繁,运行效率低
    3.游标没有关闭。
      

  4.   

    奇怪楼主的declare怎么能放到中间的