+ -
当前位置:首页 → 问答吧 → [求教] mysql 触发器制作过程中遇到的逻辑混乱

[求教] mysql 触发器制作过程中遇到的逻辑混乱

时间:2011-08-08

来源:互联网

zkfh 字段 保存数据格式如下" | "分隔字段,"||"分隔行
???? | ???? | ????||???? | ???? | ????||???? | ???? | ????

if length(@tmp) > 1 then
代码处 我无法理解了,我制作时设想是,如果没有值那么直接添入新值.
如果有值那么判断是否是第一行 如果不是第一行那么要在组合字符串前增加 || .
混乱啊,还有前面的while 循环 我无法确定它是否在工作.

SQL code
-- 新品入库
        -- 设置tmp变量 保存 当前番号下的在库番号字段
        set @tmp =(select zkfh from bupin_fh where bpfh=new.bpfh);
        set @i=0;
        -- 输出字符
        set @ostr="";
        
        -- set @tmpl=length(@tmp)-length(replace(@tmp,"||",''));
        -- 便利tmp中的所有行以(‖)分隔
        while @i < func_get_split_string_total(@tmp,"||")  do
        -- while @i < @tmpl do
            -- 将获得的一行数据保存入变量xstr1
            set @xstr1 = func_get_split_string(@tmp,"||",@i+1);
            -- 修正后的数据行
            set @xstr2="";
            -- 番号变量
            set @fh = func_get_split_string(@xstr1," | ",1);
            -- 供应商id变量
            set @gys = func_get_split_string(@xstr1," | ",2);
            -- 数量 变量
            set @sl = func_get_split_string(@xstr1," | ",2);
            -- 检查zkfh 列内是否含有当前番号
            if @fh = new.bpfh and @gys = new.gys_id then
                -- 数据格式 "番号 | 供应商id | 数量"
                set @xstr2=concat_ws(" | ", @fh , @gys , @sl + new.sl);
            else
                -- 如果部品番号不符合就将原由数据返回
                set @xstr2=@xstr1;
            end if;

            -- 重组数据
            set @j=0;
            -- 遍历 字段
            while @j < func_get_split_string_total(@tmp,"||")  do
            -- while @j < @tmpl do
                -- 当和上级循环便历同一行时,覆盖新数据.并存入 ostr变量中
                if @j=@i then
                    -- 如果是第一行,直接覆盖.否则在数据行前增加分隔符号 "||"
                    if @j=0 then
                        set @ostr=@xstr2;
                    else
                        set @ostr=concat(@ostr,concat("||",@xstr2));
                    end if;
                else
                    if @j=0 then
                        set @ostr=@xstr1;
                    else
                        set @ostr=concat(@ostr,concat("||",@xstr1));
                    end if;
                end if;
                set @j=@j+1;
            end while;
            -- 数据重组完成  此时 tmp 为旧数据 ostr为新数据.
            -- 覆盖tmp
            set @tmp=@ostr;
            set @i=@i+1;
        end while;

        -- 当数据库中 zkfh 列中无数据时 直接添入新数据,如果之前以有旧数据则添入重组后的数据
        if length(@tmp) > 1 then
            if func_get_split_string_total(@tmp,"||") <= 1 then
                update bupin_fh set zkfh=@tmp,xpzks=xpzks + new.sl  where bpfh=new.bpfh;
                -- update bupin_fh set zkfh=func_get_split_string_total(@tmp,"||"),xpzks=xpzks + new.sl  where bpfh=new.bpfh;
            else
                -- update bupin_fh set zkfh=" >1 ",xpzks=xpzks + new.sl  where bpfh=new.bpfh;
                update bupin_fh set zkfh=concat_ws("||",zkfh,@tmp),xpzks=xpzks + new.sl  where bpfh=new.bpfh;
            end if;
        else
            set @outstr=concat_ws(" | ",new.bpfh,new.gys_id,new.sl);
            -- 添入重组后数据
            update bupin_fh set zkfh=@outstr,xpzks=xpzks + new.sl  where bpfh=new.bpfh;
        end if;
        
    end if;

作者: chinayeren   发布时间: 2011-08-08

就是拆分字符串?直接用SQL语句+辅助表就可以解决了

作者: wwwwb   发布时间: 2011-08-08