我试图更新表格中的多行:
Nip | AttendanceDate | InTime | OutTime
----------------------------------------------------------------------------------------------
1105321|2016-08-30 00:00:00.000|1900-01-01 00:00:00.000|1900-01-01 00:00:00.000
1105321|2016-08-31 00:00:00.000|1900-01-01 00:00:00.000|1900-01-01 00:00:00.000
1105321|2016-09-01 00:00:00.000|1900-01-01 00:00:00.000|1900-01-01 00:00:00.000
1105321|2016-09-02 00:00:00.000|1900-01-01 00:00:00.000|1900-01-01 00:00:00.000
我想更新InTime
& OutTime
。
我知道我可以做到这一点
update Attendance
set InTime = '2016-08-30 08:00:00.000',
OutTime = '2016-08-30 18:00:00.000'
where Nip = '1105321' and AttendanceDate = '2016-08-30'
但是根据我的疑问,我必须一个接一个地做。所以,我的问题是我可以只更新一次吗?所以我不需要逐个进行更新。可能吗?抱歉我的英语不好。
更新:
所以我不需要这样做
update Attendance
set InTime = '2016-08-30 08:00:00.000',
OutTime = '2016-08-30 18:00:00.000'
where Nip = '1105321' and AttendanceDate = '2016-08-30'
update Attendance
set InTime = '2016-08-31 08:00:00.000',
OutTime = '2016-08-31 18:00:00.000'
where Nip = '1105321' and AttendanceDate = '2016-08-31'
update Attendance
set InTime = '2016-09-01 08:00:00.000',
OutTime = '2016-09-01 18:00:00.000'
where Nip = '1105321' and AttendanceDate = '2016-09-01'
update Attendance
set InTime = '2016-09-02 08:00:00.000',
OutTime = '2016-09-02 18:00:00.000'
where Nip = '1105321' and AttendanceDate = '2016-09-02'
答案 0 :(得分:1)
假设AttendanceDate
的数据类型为dateTtime
,您可以使用简单的UPDATE
语句和DATEADD
函数:
UPDATE Attendance
SET InTime = DATEADD(HOUR, 8, AttendanceDate),
OutTime = DATEADD(HOUR, 18, AttendanceDate)
WHERE Nip = '1105321'
该语句更新满足条件Nip = '1105321'
的所有行(由WHERE
语句定义)。如果您想要更新每一行,只需删除WHERE
语句。
InTime
的计算方法是向AttendanceDate
和OutTime
添加8小时,向AttendanceDate
添加18小时。
如果将日期存储为字符串,则需要从字符串到datetime
进行一些转换,然后在完成后转换,或者选择使用字符串操作。