zkfh 字段 保存数据格式如下" | "分隔字段,"||"分隔行
???? | ???? | ????||???? | ???? | ????||???? | ???? | ????if length(@tmp) > 1 then
代码处 我无法理解了,我制作时设想是,如果没有值那么直接添入新值.
如果有值那么判断是否是第一行 如果不是第一行那么要在组合字符串前增加 || .
混乱啊,还有前面的while 循环 我无法确定它是否在工作.-- 新品入库
-- 设置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;
???? | ???? | ????||???? | ???? | ????||???? | ???? | ????if length(@tmp) > 1 then
代码处 我无法理解了,我制作时设想是,如果没有值那么直接添入新值.
如果有值那么判断是否是第一行 如果不是第一行那么要在组合字符串前增加 || .
混乱啊,还有前面的while 循环 我无法确定它是否在工作.-- 新品入库
-- 设置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;
???? | ???? | ????||???? | ???? | ????||???? | ???? | ????
比如是 A1-18-05 | 6 | 1||A1-18-05 | 5 | 3
第一个| 前是 翻号 第二个是 供应商id 第三个是数量
new.bpfh new.gys_id new.sl
分别是上面三个
要实现的功能是,如果zkfh 这个字段原来没有数据,那么就直接把新数据 按照上面的格式添入
如果有数据那么判断 是否 番号 和 供应商id是否一致,如果一致 那么把数量直接相加起来,然后写回字段.
如果是不同番号或者不同供应商 那么就 直接在字段尾部 增加新 数据内容.
目前我经过多次测试发现 if @fh = new.bpfh and @gys = new.gys_id then
永返回的结果 总是我无法理解啊崩溃,这个触发我写了3天了 一点进展也没有 ~~~~~
Navicat MySQL Data TransferSource Server : my sqlserver
Source Server Version : 50148
Source Host : localhost:3306
Source Database : bupinTarget Server Type : MYSQL
Target Server Version : 50148
File Encoding : 65001Date: 2011-08-08 13:00:17
*/SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `bupin_fh`
-- ----------------------------
DROP TABLE IF EXISTS `bupin_fh`;
CREATE TABLE `bupin_fh` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`bpfh` varchar(12) NOT NULL,
`bpmcc` varchar(64) NOT NULL,
`bpmcj` varchar(64) NOT NULL,
`bpdj` int(11) NOT NULL,
`bpdgd` tinyint(4) NOT NULL,
`bpjhq` tinyint(4) NOT NULL,
`adduser` varchar(16) DEFAULT 'SYSTEM' COMMENT '作操员名字',
`bptime` datetime DEFAULT NULL,
`sjfh` varchar(16) DEFAULT NULL,
`xpzks` int(11) DEFAULT '0' COMMENT '新品在库量数',
`zgpzks` int(11) DEFAULT '0' COMMENT '中古品在库数',
`zkfh` varchar(255) CHARACTER SET utf8 DEFAULT NULL COMMENT '在库番号',
PRIMARY KEY (`id`,`bpfh`)
) ENGINE=InnoDB AUTO_INCREMENT=325 DEFAULT CHARSET=gb2312;-- ----------------------------
-- Records of bupin_fh
-- ----------------------------
INSERT INTO `bupin_fh` VALUES ('1', 'A1-18-05', '密封圈 V-70', 'Oリング', '4', '3', '6', 'SYSTEM', '2011-07-20 09:49:16', null, '106', '3', 'A1-18-05 | 5 | 1||A1-18-05 | 5 | 3');
INSERT INTO `bupin_fh` VALUES ('102', 'B1-36-05', '皮带 150S5M375', 'ベルト', '32', '3', '6', 'SYSTEM', '2011-07-20 09:49:16', null, '2', '3', null);
INSERT INTO `bupin_fh` VALUES ('103', 'B1-37-01', '皮带 250S5M575', 'ベルト', '68', '3', '6', 'SYSTEM', '2011-07-20 09:49:16', null, '4', '2', null);
INSERT INTO `bupin_fh` VALUES ('104', 'B1-37-02', '皮带 250S5M600', 'ベルト', '71', '3', '6', 'SYSTEM', '2011-07-20 09:49:16', null, '2', '0', null);
INSERT INTO `bupin_fh` VALUES ('105', 'B1-37-03', '皮带 250S5M650', 'ベルト', '81', '3', '6', 'SYSTEM', '2011-07-20 09:49:16', null, '0', '0', null);
INSERT INTO `bupin_fh` VALUES ('106', 'B1-37-04', '皮带 250S5M830', 'ベルト', '101', '3', '6', 'SYSTEM', '2011-07-20 09:49:16', null, '0', '0', null);
INSERT INTO `bupin_fh` VALUES ('107', 'B1-38-01', '皮带 150S8M672', 'ベルト', '67', '3', '6', 'SYSTEM', '2011-07-20 09:49:16', null, '0', '0', null);
INSERT INTO `bupin_fh` VALUES ('108', 'B1-38-02', '皮带 150S8M712', 'ベルト', '71', '3', '6', 'SYSTEM', '2011-07-20 09:49:16', null, '0', '0', null);
INSERT INTO `bupin_fh` VALUES ('109', 'B1-38-03', '皮带 150S8M1520', 'ベルト', '90', '3', '6', 'SYSTEM', '2011-07-20 09:49:16', null, '0', '0', null);
INSERT INTO `bupin_fh` VALUES ('110', 'B1-38-04', '皮带 150S8M1912', 'ベルト', '158', '3', '6', 'SYSTEM', '2011-07-20 09:49:16', null, '0', '0', null);
INSERT INTO `bupin_fh` VALUES ('111', 'B1-38-05', '皮带 150DS8M3680', 'ベルト', '860', '3', '6', 'SYSTEM', '2011-07-20 09:49:16', null, '2', '0', null);
INSERT INTO `bupin_fh` VALUES ('112', 'B1-39-01', '皮带 250S8M560', 'ベルト', '72', '3', '6', 'SYSTEM', '2011-07-20 09:49:16', null, '0', '0', null);
INSERT INTO `bupin_fh` VALUES ('113', 'B1-39-02', '皮带 250S8M632', 'ベルト', '77', '3', '6', 'SYSTEM', '2011-07-20 09:49:16', null, '0', '0', null);
INSERT INTO `bupin_fh` VALUES ('114', 'B1-39-03', '皮带 250S8M712', 'ベルト', '118', '3', '6', 'SYSTEM', '2011-07-20 09:49:16', null, '0', '0', null);
INSERT INTO `bupin_fh` VALUES ('115', 'B1-39-04', '皮带 250S8M896', 'ベルト', '101', '3', '6', 'SYSTEM', '2011-07-20 09:49:16', null, '0', '0', null);
INSERT INTO `bupin_fh` VALUES ('116', 'B1-40-01', '皮带 250S8M1000', 'ベルト', '110', '3', '6', 'SYSTEM', '2011-07-20 09:49:16', null, '0', '0', null);
INSERT INTO `bupin_fh` VALUES ('117', 'B1-40-02', '皮带 250S8M1152', 'ベルト', '122', '3', '6', 'SYSTEM', '2011-07-20 09:49:16', null, '0', '0', null);
INSERT INTO `bupin_fh` VALUES ('118', 'B1-40-03', '皮带 250S8M1160', 'ベルト', '118', '3', '6', 'SYSTEM', '2011-07-20 09:49:16', null, '0', '0', null);
INSERT INTO `bupin_fh` VALUES ('119', 'B1-40-04', '皮带 250S8M1280', 'ベルト', '126', '3', '6', 'SYSTEM', '2011-07-20 09:49:16', null, '0', '0', null);
INSERT INTO `bupin_fh` VALUES ('120', 'B1-41-01', '皮带 250S8M1264', 'ベルト', '136', '3', '6', 'SYSTEM', '2011-07-20 09:49:16', null, '0', '0', null);
INSERT INTO `bupin_fh` VALUES ('121', 'B1-42-01', '皮带 250S8M1344', 'ベルト', '136', '3', '6', 'SYSTEM', '2011-07-20 09:49:16', null, '0', '0', null);
INSERT INTO `bupin_fh` VALUES ('122', 'B1-42-02', '皮带 250S8M1480', 'ベルト', '144', '3', '6', 'SYSTEM', '2011-07-20 09:49:16', null, '0', '0', null);
INSERT INTO `bupin_fh` VALUES ('322', 'B2-32-07', '轴套 LM20UU', 'プッシュ', '117', '3', '6', 'SYSTEM', '2011-07-20 09:49:16', null, '0', '0', null);
下面是我的触发全部
先实现新品入库功能,其他部分只要少许修改代码就可以实现了.drop trigger if exists bpadd;
create trigger bpadd
after insert on bupin_inout
for each row begin
set character_set_client = gb2312;
set character_set_results = gb2312;
set character_set_connection = gb2312;
-- 判断是入库还是出库
if new.churu=0 then
-- 入库的时候判断 是中古品还是新品
if upper(left(new.bpfh,1))="C" then
-- 中古品入库
set @tmp=0;
else
-- 新品入库
-- 设置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,"||")-1 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," | ",3);
-- 检查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,"||")-1 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,"||") = 0 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;
elseif func_get_split_string_total(@tmp,"||") =1 then
set @outstr=concat_ws(" | ",new.bpfh,new.gys_id,new.sl);
-- 添入重组后数据
update bupin_fh set zkfh=concat_ws("||",zkfh,@outstr),xpzks=xpzks + new.sl where bpfh=new.bpfh;
else
-- update bupin_fh set zkfh=concat_ws("||",zkfh,@tmp),xpzks=xpzks + new.sl where bpfh=new.bpfh;
update bupin_fh set 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;
else
-- 出库部分
set @tmp=0;
end if;
end
CREATE TABLE `bupin_inout` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`bpfh` varchar(12) NOT NULL,
`churu` tinyint(4) NOT NULL DEFAULT '0' COMMENT '出入库0为入库1为出库',
`sl` int(11) NOT NULL COMMENT '数量',
`bgcs` varchar(32) NOT NULL COMMENT '保管场所',
`gys_id` int(11) NOT NULL COMMENT '应商供id',
`fdate` date NOT NULL,
`bphs` tinyint(4) NOT NULL COMMENT '旧品是否回收 0不回收 1回收',
`adduser` varchar(16) NOT NULL,
`sjfh` varchar(16) DEFAULT NULL COMMENT '级上番号',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=243 DEFAULT CHARSET=gb2312;
现在还有一个问题,在zkfh以有三个不同 字段(用||分隔的)已经可以累计计算了,但是 会把用||分隔的最后一段多增加一才. 但是我看代码 不应该啊~~~~ 没看出问题来.
谁能帮我看看[code=SQL]-- 新品入库
-- 设置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,"||")-1 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," | ",3);
-- 检查zkfh 列内是否含有当前番号
if @fh = new.bpfh and @gys = new.gys_id then
-- 数据格式 "番号 | 供应商id | 数量"
set @xstr2=concat_ws(" | ", @fh , @gys , @sl + new.sl);
set @tmpl=@tmpl+1;
else
-- 如果部品番号不符合就将原由数据返回
set @xstr2=@xstr1;
set @tmpl=@tmpl-1;
end if; if @i = 0 then
set @ostr=@xstr2;
else
set @ostr=concat_ws("||",@ostr,@xstr2);
end if;
set @i=@i+1;
end while;
-- 如果 进入过循环 那么赋值
if @ostr<>"" then
-- 覆盖tmp
set @tmp=@ostr;
end if; if @tmpl=0 or @ostr="" then
-- 将获得的一行数据保存入变量xstr1
set @xstr1 = func_get_split_string(@tmp,"||",1);
-- 番号变量
set @fh = func_get_split_string(@xstr1," | ",1);
-- 供应商id变量
set @gys = func_get_split_string(@xstr1," | ",2);
-- 数量 变量
set @sl = func_get_split_string(@xstr1," | ",3);
-- 检查zkfh 列内是否含有当前番号
if @fh = new.bpfh and @gys = new.gys_id then
set @tmp=concat_ws(" | ",new.bpfh,new.gys_id,@sl+new.sl);
else
set @tmp=concat(@tmp,concat("||",concat_ws(" | ",new.bpfh,new.gys_id,new.sl)));
end if;
end if; -- 当数据库中 zkfh 列中无数据时 直接添入新数据,如果之前以有旧数据则添入重组后的数据
if length(@tmp) > 1 then
-- 本来有数据
update bupin_fh set zkfh=@tmp,xpzks=xpzks + new.sl where bpfh=new.bpfh;
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;[/code