【发布时间】:2017-03-26 06:08:34
【问题描述】:
我有 2 张桌子,ShareButton 和 SharePage。
ShareButton表:
+----+---------------+---------------+
| ID | Name | TotalShare |
+----+---------------+---------------+
| 1 | Facebook | 0 |
| 2 | Twitter | 0 |
+----+---------------+---------------+
SharePage表:
+----+--------------------+-------+---------------+
| ID | URL | Share | ShareButtonID |
+----+--------------------+-------+---------------+
| 1 | www.abc.xyz/page1 | 3 | 1 |
| 2 | www.abc.xyz/page1 | 14 | 2 |
| 3 | www.abc.xyz/page2 | 6 | 1 |
| 4 | www.abc.xyz/page2 | 10 | 2 |
+----+--------------------+-------+---------------+
在SharePage表中插入或更新一条记录后,ShareButton的TotalShare列被更新
update ShareButton
set TotalShare = (sum(Share) from SharePage where "ShareButtonID" = ShareButtonID of updated/inserted record))
where ID = ShareButtonID of updated/inserted record)`
感谢阅读!
【问题讨论】:
-
为什么还要保存这些信息?为什么不在需要的时候用聚合查询来查询呢?
-
你做的太多了+为什么触发?您可以简单地更新存储过程中修改
SharePage表的父表。 -
谢谢大家。这是最好和最简单的方法:D
标签: sql-server triggers sql-update sql-insert