我有一个历史表,数据如下:
在我的情况下,product id
(110,111,112)更改了price
,因为我拥有与product_updateDate
相同的产品,有时我可以使用相同的重复数据没有信息改变
+------+------------+--------------+---------------+-----------------------------+-------------------------+-------------------------+----------------+
| id | product_id | product_name | product_Price | product_addDate | product_UpdateDate | Insertdate_DB | Updatedate_DB |
+------+------------+--------------+---------------+-----------------------------+-------------------------+-------------------------+----------------+
| 1 | 110 | DELL | 1000 | 2017-03-01 08:00:00.000 | NULL | 2017-03-06 10:00:00.000 | NULL |
| 2 | 111 | HP | 900 | 2017-03-01 08:00:00.000 | NULL | 2017-03-06 10:00:00.000 | NULL |
| 3 | 112 | Mac | 1300 | 2017-03-01 08:00:00.000 | NULL | 2017-03-06 10:00:00.000 | NULL |
| 4 | 113 | Lenovo | 950 | 2017-03-01 08:00:00.000 | NULL | 2017-03-06 10:00:00.000 | NULL |
| 5 | 110 | DELL | 900 | 2017-03-04 08:00:00.000 | 2017-03-04 08:00:00.000 | 2017-03-07 10:00:00.000 | NULL |
| 6 | 111 | HP | 800 | 2017-03-04 08:00:00.000 | 2017-03-04 08:00:00.000 | 2017-03-07 10:00:00.000 | NULL |
| 7 | 112 | Mac | 120 | 2017-03-04 08:00:00.000 | 2017-03-04 08:00:00.000 | 2017-03-07 10:00:00.000 | NULL |
+------+------------+--------------+---------------+-----------------------------+-------------------------+-------------------------+----------------+
我想要的是进行更新查询以获得结果:
+----+------------+--------------+---------------+-------------------------+-------------------------+-------------------------+---------------+
| id | product_id | product_name | product_Price | product_addDate | product_UpdateDate | Insertdate_DB | Updatedate_DB |
+----+------------+--------------+---------------+-------------------------+-------------------------+-------------------------+---------------+
| 1 | 110 | DELL | 1000 | 2017-03-01 08:00:00.000 | 2017-03-04 08:00:00.000 | 2017-03-06 10:00:00.000 | GETDATE() |
| 2 | 111 | HP | 900 | 2017-03-01 08:00:00.000 | 2017-03-04 08:00:00.000 | 2017-03-06 10:00:00.000 | GETDATE() |
| 3 | 112 | Mac | 1300 | 2017-03-01 08:00:00.000 | 2017-03-04 08:00:00.000 | 2017-03-06 10:00:00.000 | GETDATE() |
| 4 | 113 | Lenovo | 950 | 2017-03-01 08:00:00.000 | NULL | 2017-03-06 10:00:00.000 | NULL |
| 5 | 110 | DELL | 900 | 2017-03-04 08:00:00.000 | NULL | 2017-03-07 10:00:00.000 | NULL |
| 6 | 111 | HP | 800 | 2017-03-04 08:00:00.000 | NULL | 2017-03-07 10:00:00.000 | NULL |
| 7 | 112 | Mac | 120 | 2017-03-04 08:00:00.000 | NULL | 2017-03-07 10:00:00.000 | NULL |
+----+------------+--------------+---------------+-------------------------+-------------------------+-------------------------+---------------+
以下查询可以更新product_UpdateDate
update tablename set product_UpdateDate=
case when tablename.product_UpdateDate is null then t.product_UpdateDate
else null end
from tablename
join tablename t on tablename.product_id=t.product_id
and t.ID<>tablename.id `
但是,当我尝试更新此列时,我想更改Updatedate_DB
GETDATE()
值,所有行的值都为GETDATE()
,但我只想更改行的id | product_name | category_id | created_at
价值有变化
感谢您的帮助
答案 0 :(得分:0)
我不确定我是否理解您的问题,但如果您只想更改您编写日期的行(而不是null),则可以将其添加到您的更新中:
, UPDATEDATE_DB=CASE WHEN TABLENAME.PRODUCT_UPDATEDATE IS NULL THEN 'GETDATE()'
ELSE TABLENAME.UPDATEDATE_DB END