多个Table触发器Update/Delete/Insert

Multiple Table Trigger Update/Delete/Insert

我在创建 SQL 服务器触发器来执行我想要的操作时遇到了问题。我对触发器没有太多经验。

基本上我有一个 table,我们称它为 cluster_metadata,其中包含描述对象某些属性的元数据。然后我有第二个 table,我们称它为 activities_table,用户输入的数据可能与 cluster_metadata table 中的某些对象有关。

cluster_metadata table 是用户updatable 但是使用存储过程创建和删除新行,用户只能更新特定值。

activities_table 完全由用户驱动,用户可以insert/modify 和删除行。

我需要一个触发器来连接数据并在 cluster_metadataactivities_table.

的任何修改时更新 table

为简单起见,我减少了列数,但 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  |
+--------------------------------------------+

我需要创建并保持更新的 table 使用上述两个 table 中的数据看起来像这样:

+---------------------------------------------------------------------+
|  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  |
+---------------------------------------------------------------------+

我将如何创建执行此操作的触发器?我只想创建一个视图,但使用此合并数据我需要接受一些用户额外输入。

谢谢!

感谢您的帮助。基本上,我最终所做的是使用来自 cluster_metadataactivities_table 的数据创建了一个联合视图。从那里我编写了一个存储过程,它获取适当的数据并插入第三个 table。该过程还确保所有数据都已更新并与每次执行的视图相匹配。每次用户从网络 UI 向 table 中的任何一个输入任何内容时,我都会从那里 运行 执行该过程。不是最好的解决方案,但它的工作。

谢谢大家!