我可以选择具有多个更新触发器的所有表

时间:2009-11-26 23:09:25

标签: sql-server sql-server-2005 sql-server-2008

我可以选择具有多个更新触发器的所有表吗?以下代码选择具有多个触发器的所有表,但我只需要具有多个更新触发器的表。 此查询还返回具有一个插入触发器和一个更新一个的表,这不是我想要的。

SELECT * FROM sys.triggers
WHERE parent_id IN(SELECT parent_id
FROM sys.triggers
GROUP BY parent_id
HAVING COUNT(*)>1)

2 个答案:

答案 0 :(得分:3)

您应该能够从sys.trigger_events中告诉它,例如:

SELECT *
  FROM sys.trigger_events AS te
  INNER JOIN sys.triggers AS t
  ON t.[object_id] = te.[object_id]
  WHERE te.[type] IN (1,2,3);

因此要获得具有多个更新触发器的表:

SELECT OBJECT_NAME(parent_id)
  FROM sys.triggers AS t
  INNER JOIN sys.trigger_events AS te
  ON t.[object_id] = te.[object_id]
  WHERE te.type_desc = 'UPDATE'
  GROUP BY OBJECT_NAME(parent_id)
  HAVING COUNT(*) > 1;

答案 1 :(得分:0)

不太可靠,但如果您需要快速而肮脏的东西,它应该可以使用。

SELECT * FROM sys.triggers
WHERE parent_id IN
(SELECT parent_id
FROM sys.triggers tt
JOIN sys.syscomments sc on sc.id=tt.object_id
WHERE sc.text LIKE '%AFTER UPDATE%'
GROUP BY parent_id
HAVING COUNT(*)>1)