我有创建SQL Server触发器来做我想做的麻烦。触发器我没有太多经验。多表触发器更新/删除/插入
基本上我有一个表,我们称它为cluster_metadata
,其中包含描述某个对象某些属性的元数据。然后我有第二个表,我们称之为activities_table
,用户输入的数据可能与cluster_metadata
表中的某些对象有关。
cluster_metadata
表是用户可更新的,但是使用存储过程创建和删除新行,用户只能更新特定值。
activities_table
完全由用户驱动,用户可以插入/修改和删除行。
我需要一个触发器来加入数据,并将更新表的任何修改cluster_metadata
或activities_table
。
为了简单起见,我已经减少了列数,但表格看起来像这样。
cluster_metadata
:
+----------------------------------+
| Cluster | Eligible | Group |
+----------------------------------+
| Cluster1 | True | 1 |
| Cluster2 | True | 1 |
| Cluster3 | True | 2 |
| Cluster4 | False | 2 |
| Cluster5 | True | 3 |
| Cluster6 | True | 4 |
+----------------------------------+
activities_table
:
+--------------------------------------------+
| Activity | ID | Group | Start Date |
+--------------------------------------------+
| Patches | 1000 | 1 | 02-01-2015 |
| Patches | 1000 | 2 | 02-10-2015 |
| Patches | 1000 | 3 | 02-20-2015 |
|SomeActivity| 1001 | 2 | 02-30-2015 |
+--------------------------------------------+
,我需要创建并保持更新将使用从以上两个表中的数据看起来像这样的表:
+---------------------------------------------------------------------+
| Cluster | Eligible | Group | Activity | ID | Start Date |
+---------------------------------------------------------------------+
| Cluster1 | True | 1 | Patches | 1000 | 02-01-2015 |
| Cluster2 | True | 1 | Patches | 1000 | 02-01-2015 |
| Cluster3 | True | 2 | Patches | 1000 | 02-10-2015 |
| Cluster3 | True | 2 |SomeActivity| 1001 | 02-30-2015 |
| Cluster4 | True | 2 | Patches | 1000 | 02-10-2015 |
| Cluster4 | True | 2 |SomeActivity| 1001 | 02-30-2015 |
| Cluster5 | True | 3 | Patches | 1000 | 02-20-2015 |
+---------------------------------------------------------------------+
我将如何创建一个触发器来做到这一点?我只是创建一个视图,但有一些用户额外的输入,我需要接受使用这个合并的数据。
谢谢!
为什么你需要一个触发器来更新第三个表?为什么不创建一个代表两个表连接在一起的视图? – d89761 2015-02-24 21:18:43
将问题看作是学术问题,您需要在表1和表2中引发触发器,每个触发器都将插入和删除的表与第三个表进行比较,并对该表执行所需的插入/更新/删除操作。 – 2015-02-24 21:24:28
将“附加输入”保留在第三个表中,并在视图中加入这3个表? – MWillemse 2015-02-24 21:47:16