需要列出 SQL Server 数据库中的所有触发器以及表名和表架构
我需要列出 SQL Server 数据库中的所有触发器以及表名称和表架构。
我几乎已经完成了:
SELECT trigger_name = name, trigger_owner = USER_NAME(uid),table_schema = , table_name = OBJECT_NAME(parent_obj),
isupdate = OBJECTPROPERTY( id, 'ExecIsUpdateTrigger'), isdelete = OBJECTPROPERTY( id, 'ExecIsDeleteTrigger'),
isinsert = OBJECTPROPERTY( id, 'ExecIsInsertTrigger'), isafter = OBJECTPROPERTY( id, 'ExecIsAfterTrigger'),
isinsteadof = OBJECTPROPERTY( id, 'ExecIsInsteadOfTrigger'),
[disabled] = OBJECTPROPERTY(id, 'ExecIsTriggerDisabled')
FROM sysobjects INNER JOIN sysusers ON sysobjects.uid = sysusers.uid
WHERE type = 'TR'
我只需要获取表的架构。
I need to list all triggers in SQL Server database with table name and table's schema.
I'm almost there with this:
SELECT trigger_name = name, trigger_owner = USER_NAME(uid),table_schema = , table_name = OBJECT_NAME(parent_obj),
isupdate = OBJECTPROPERTY( id, 'ExecIsUpdateTrigger'), isdelete = OBJECTPROPERTY( id, 'ExecIsDeleteTrigger'),
isinsert = OBJECTPROPERTY( id, 'ExecIsInsertTrigger'), isafter = OBJECTPROPERTY( id, 'ExecIsAfterTrigger'),
isinsteadof = OBJECTPROPERTY( id, 'ExecIsInsteadOfTrigger'),
[disabled] = OBJECTPROPERTY(id, 'ExecIsTriggerDisabled')
FROM sysobjects INNER JOIN sysusers ON sysobjects.uid = sysusers.uid
WHERE type = 'TR'
I just need to get the table's schema also.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
你对此有何看法:非常短而简洁:)
And what do you think about this: Very short and neat :)
无需与其他表连接...所有信息都可以从 sys.objects 中获取。
No need to join with other tables... all info can be obtained from sys.objects.
这对我有用
this working for me
这就是我使用的(通常包裹在我在模型中填充的东西中):
正如你所看到的,它是一个更多的 McGyver,但我认为这是值得的:(
向右滚动查看最后也是最有用的专栏)
This is what I use (usually wrapped in something I stuff in Model):
As you see it is a skosh more McGyver, but I think it's worth it:
(Scroll right to see the final and most useful column)
使用此查询:
Use This Query :
死灵术。
只是发布,因为到目前为止所有解决方案都不够完整。
Necromancing.
Just posting because all solutions so far fall a bit short of completeness.
这可能会有所帮助。
this may help.
上面的代码是不正确的,如下所示:
The just above code is incorrect as shown:
C# Cribs:我最终得到了这个超级通用的衬垫。希望这对原始发布者和/或刚刚在 Google 中输入相同问题的人有用:
查询特征:
C# Cribs: I ended up with this super generic one liner. Hope this is useful to both the original poster and/or people who just typed the same question I did into Google:
Query Characteristics:
如果您正在寻找
ALL
触发器,请记住 MS-SQL 具有基于 SQL 的触发器 (sysobjects.type = 'TR'
) 和基于 CLR 的触发器 (sysobjects.type = 'TA'
)。If you are looking for
ALL
triggers, remember MS-SQL has both SQL-based triggers (sysobjects.type = 'TR'
) and CLR-based triggers (sysobjects.type = 'TA'
).一个困难是文本或描述有换行。我笨拙的拼凑,为了让它变得更表格化,是将一个
HTML
文字添加到SELECT
子句中,将所有内容复制并粘贴到记事本中,用 html 扩展名保存,在浏览器中打开,然后复制并粘贴到电子表格中。例如,
您可能仍然需要使用选项卡将描述放入一个字段中,但至少它会在一行上,我发现这非常有帮助。
One difficulty is that the text, or description has line feeds. My clumsy kludge, to get it in something more tabular, is to add an
HTML
literal to theSELECT
clause, copy and paste everything to notepad, save with an html extension, open in a browser, then copy and paste to a spreadsheet.example
you may still need to fool around with tabs to get the description into one field, but at least it'll be on one line, which I find very helpful.
这是一种方法:
编辑:
注释掉了 sysusers 的连接,以便在 AdventureWorks2008 上进行查询。
编辑 2:对于 SQL 2000
Here's one way:
EDIT:
Commented out join to sysusers for query to work on AdventureWorks2008.
EDIT 2: For SQL 2000
干得好。
这里有几件事......
我还看到您试图提取父表架构信息,我相信为了这样做,您还需要将 sysobjects 表本身加入,以便您可以正确获取架构信息对于父表。上面的查询就是这样做的。此外,结果中不需要 sysusers 表,因此 Join 已被删除。
使用 SQL 2000、SQL 2005 和 SQL 2008 R2 进行测试
Here you go.
A couple of things here...
Also I see that you were attempting to pull the parent tables schema information, I believe in order to do so you would also need to join the sysobjects table on itself so that you can correctly get the schema information for the parent table. the query above does this. Also the sysusers table wasn't needed in the results so that Join has been removed.
tested with SQL 2000, SQL 2005, and SQL 2008 R2
您还可以获取触发器的正文,如下所示:
You can also get the body of triggers as following:
我最近有同样的任务,我对 sql server 2012 db 使用了以下内容。使用 Management Studio 并连接到您要搜索的数据库。然后执行以下脚本。
I had the same task recently and I used the following for sql server 2012 db. Use management studio and connect to the database you want to search. Then execute the following script.
使用此查询:
它简单且有用。
Use this query :
It's simple and useful.