我写了下面的存储过程,它插入特定日期的记录。如果日期是重复的,则应更新值。但是在为同一日期插入新记录时,如果任何值为null,则表示该日期的已插入值也为null。 我的存储过程是:
CREATE PROCEDURE sp_setrecords (IN daten date,IN daystart time,IN lunchstart time,IN lunchend time,IN breakstart time,IN breakend time,IN dayend time,IN casualleave time,IN sickleave time,IN holidays time,IN leavewithoutpay time)
BEGIN
insert into dailyrecord (date,daystart,lunchstart,lunchend,breakstart,breakend,dayend,casualleave,sickleave,holidays,leavewithoutpay) values (daten,daystart,lunchstart,lunchend,breakstart,breakend,dayend,casualleave,sickleave,holidays,leavewithoutpay)
ON DUPLICATE KEY UPDATE
date = COALESCE(VALUES(date)),
daystart = COALESCE(VALUES(daystart)),
lunchstart = COALESCE(VALUES(lunchstart)),
lunchend = COALESCE(VALUES(lunchend)),
breakstart = COALESCE(VALUES(breakstart)),
breakend = COALESCE(VALUES(breakend)),
dayend = COALESCE(VALUES(dayend)),
casualleave = COALESCE(VALUES(casualleave)),
sickleave = COALESCE(VALUES(sickleave)),
holidays = COALESCE(VALUES(holidays)),
leavewithoutpay = COALESCE(VALUES(leavewithoutpay));
END
示例:初始值为: “2018年1月2日”, “9时三十分00秒”,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL“
然后,如果我在第二个位置插入同一日期的13:30:30,则返回:
“2018年1月2日”,NULL, “十三时30分00秒”,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL“
如果值不为空,我该如何更新值?
答案 0 :(得分:0)
您的COALESCE()
需要其他参数:
ON DUPLICATE KEY UPDATE
date = COALESCE(VALUES(date), date),
daystart = COALESCE(VALUES(daystart), daystart),
lunchstart = COALESCE(VALUES(lunchstart), lunchstart),
. . .
这将首先保留新值,然后保留现有值。