MySQL 中的存储过程用 space 空值替换零值
Stored procedure in MySQL replace zero value with an space empty
这是我在数据库 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,我将 2tparag
变量值上的新行 ('\r' OR '\n' OR '\r\n')
替换为 space
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 = \'',REPLACE(2tparag,'\r' OR '\n' OR '\r\n', ' '),'\'
WHERE sID = \'',2tid,'\';');
PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
当我在数据库的 2tparag
变量上写入 2021
时,它用 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 只会替换 一个 字符,所以只有 [= 的 pmne 17=]、\r
或\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
db<>fiddle here
这是我在数据库 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,我将 2tparag
变量值上的新行 ('\r' OR '\n' OR '\r\n')
替换为 space
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 = \'',REPLACE(2tparag,'\r' OR '\n' OR '\r\n', ' '),'\'
WHERE sID = \'',2tid,'\';');
PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
当我在数据库的 2tparag
变量上写入 2021
时,它用 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 只会替换 一个 字符,所以只有 [= 的 pmne 17=]、\r
或\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
db<>fiddle here