标签:
项目中用到的,要判断表中某个字段的某几位,若为某个值则替换,用到了几个典型的字符串操作,记录备注实现方案如下:
备注:如果替代字符串是唯一的话,可以用replace,这里用的是concat拼接。
DELIMITER // DROP PROCEDURE IF EXISTS p_modify_year; CREATE PROCEDURE p_modify_year() BEGIN DECLARE tmpid int(8); DECLARE tmppcidd varchar(20); DECLARE tmpprocode varchar(20); DECLARE done INT DEFAULT FALSE; DECLARE cur CURSOR FOR SELECT product.id FROM product WHERE prostatus=0; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; OPEN cur; myloop: LOOP FETCH cur INTO tmpid; IF done THEN LEAVE myloop; END IF; SELECT SUBSTRING(product.pcidd,2,2), product.procode INTO tmppcidd, tmpprocode FROM product WHERE id = tmpid;
//pcidd和procode中第2、3位为16则替换成AU,其中procode可能为空。 IF tmppcidd = ‘16‘ THEN UPDATE upe_product set pcidd = CONCAT(SUBSTRING(pcidd, 1, 1), ‘AU‘, SUBSTRING(pcidd, 4, LENGTH(pcidd)+1)) WHERE id = tmpid; IF !ISNULL(tmpprocode) THEN UPDATE product set procode = CONCAT(SUBSTRING(procode, 1, 1), ‘AU‘, SUBSTRING(procode, 4, LENGTH(procode)+1)) WHERE id = tmpid; END IF; END IF; SET tmpprocode = NULL; SET tmppcidd = NULL; SET done = FALSE; END LOOP; CLOSE cur; END //
标签:
原文地址:http://www.cnblogs.com/Fredric-2013/p/5065091.html