大佬教程收集整理的这篇文章主要介绍了MySQL中的存储过程用空格替换零值,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我在数据库 MysqL 版本 8.0.17 上的存储过程
CREATE defineR=`root`@`%` PROCEDURE `SP`(IN tun CHAR(100),tmonth int(2),tparag LONGTEXT,tID int(11))
BEGIN
declare 2tun char(100);
declare 2tmonth int(2);
declare 2tyear int(4);
DECLARE 2tparag LONGTEXT;
DECLARE 2tID INT(11);
SET 2tun = tun;
SET 2tmonth = tmonth;
SET 2tyear = YEAR(CURDATE());
SET 2tparag = tparag;
SET 2tID = tID;
SET @s = CONCAT('updatE t_contents_',2tun,'_',2tmonth,2tyear,'
SET contents = \'',2tparag,'\'
WHERE sID = \'',2tID,'\';');
PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
此代码工作正常。
但是如果尝试使用这个其他版本的 SP,我用空格替换 ('\r' OR '\n' OR '\r\n')
变量值上的新行 2tparag
CREATE defineR=`root`@`%` PROCEDURE `SP`(IN tun CHAR(100),replaCE(2tparag,'\r' OR '\n' OR '\r\n',' '),'\'
WHERE sID = \'','\';');
PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
当我在数据库的 2021
变量上写入 2tparag
时,它会用 2 21
值更新该字段...
如何解决这个问题?
示例
起始字符串是
Neque porro quisquam est qui dolorem ipsum
通过添加当前年份更新此字符串
Neque porro quisquam est qui dolorem ipsum 2021
在数据库上存储
Neque porro quisquam est qui dolorem ipsum 2 21
您的查询没有任何问题
您的 REPALCE
不会像您预期的那样工作,Mysql 只会替换 one 次出现的字符,因此只有 \n
、\r
的 pmne或\n\r
修复您需要使用的所有事件
replaCE(replaCE(replaCE('test\na\rb','\r',' '),'\n','\r\n',' ')
create table t_contents_123_2_2021(sID int,contents LONGTEXT)
INSERT INTO t_contents_123_2_2021 VALUEs(1,'test')
CREATE PROCEDURE `SP`(IN tun CHAR(100),tmonth int(2),tparag LONGTEXT,tid int(11))
BEGIN
declare 2tun char(100);
declare 2tmonth int(2);
declare 2tyear int(4);
DECLARE 2tparag LONGTEXT;
DECLARE 2tid INT(11);
SET 2tun = tun;
SET 2tmonth = tmonth;
SET 2tyear = YEAR(CURDATE());
SET 2tparag = tparag;
SET 2tid = tid;
SET @s = CONCAT('updatE t_contents_',2tun,'_',2tmonth,2tyear,'
SET contents = \'',replaCE(2tparag,'\r' OR '\n' OR '\r\n','\'
WHERE sID = \'',2tid,'\';');
PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
✓
CALL SP ('123','02','Neque porro quisquam est qui dolorem ipsum',1)
SELECT * FROM t_contents_123_2_2021
sID | contents --: | :----------------------------------------- 1 | Neque porro quisquam est qui dolorem ipsum
dbfiddle here
以上是大佬教程为你收集整理的MySQL中的存储过程用空格替换零值全部内容,希望文章能够帮你解决MySQL中的存储过程用空格替换零值所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。