如何获取 SQL Server 中触发器的定义(主体)?

发布于 2024-07-04 22:31:59 字数 469 浏览 9 评论 0原文

由于无法找到满足我需求的 SQL diff 工具,我正在编写自己的工具。 在 INFORMATION_SCHEMA 和 sys 表之间,我有一个基本完整的工作版本。 但我在元数据中找不到的一件事是触发器的定义,即实际的 SQL 代码。 我是否忽略了什么?

谢谢。


谢谢,皮特,我不知道!

斯科特,我正在使用非常基本的托管包,不允许远程连接到数据库。 我不知道 RedGate 的规格(反正我买不起)他们是否提供了解决方法,尽管也有 API(例如来自 Apex 的 API),但我没有看到我认为投资一个解决方案仍然需要更多的编程。 :)

我的解决方案是在网站上放置一个 ASPX 页面,充当一种“架构服务”,以 XML 形式返回收集的元数据。 我设置了一个小型 AJAX 应用程序,它将任意数量的目录实例与主实例进行比较并显示差异。 这并不完美,但对我来说是向前迈出的重要一步。

再次感谢!

Unable to find a SQL diff tool that meets my needs, I am writing my own. Between the INFORMATION_SCHEMA and sys tables, I have a mostly-complete working version. But one thing I can't find in the metadata is the definition of a trigger, you know, the actual SQL code. Am I overlooking something?

Thanks.


Thanks, Pete, I didn't know about that!

Scott, I'm working with very basic hosting packages that don't allow remote connections to the DB. I don't know from the specs on RedGate (which I can't afford anyway) whether they provide a workaround for that, and although there are also API's out there (such as the one from Apex), I didn't see the point in investing in a solution that was still going to require more programming on my part. :)

My solution is to drop an ASPX page on the site that acts as a kind of "schema service", returning the collected metadata as XML. I set up a little AJAX app that compares any number of catalog instances to a master and shows the diffs. It's not perfect, but a major step forward for me.

Thanks again!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

っ左 2024-07-11 22:32:00

该查询返回触发器及其名称和正文。

Select 
    [tgr].[name] as [trigger name], 
    [tbl].[name] as [table name] , 
    OBJECT_DEFINITION(tgr.id) body

    from sysobjects tgr 

    join sysobjects tbl
    on tgr.parent_obj = tbl.id

WHERE tgr.xtype = 'TR'

this query return trigger with its name and body.

Select 
    [tgr].[name] as [trigger name], 
    [tbl].[name] as [table name] , 
    OBJECT_DEFINITION(tgr.id) body

    from sysobjects tgr 

    join sysobjects tbl
    on tgr.parent_obj = tbl.id

WHERE tgr.xtype = 'TR'
东走西顾 2024-07-11 22:32:00

您可以通过多种方式查看 SQL Server 触发器定义。

系统视图查询:

SELECT definition
FROM sys.sql_modules
WHERE object_id = OBJECT_ID('trigger_name');

SELECT OBJECT_NAME(parent_obj) [table name], 
   NAME [triger name], 
   OBJECT_DEFINITION(id) body
FROM sysobjects
WHERE xtype = 'TR'
  AND name = 'trigger_name';

使用OBJECT_DEFINITION函数定义:

SELECT OBJECT_DEFINITION(OBJECT_ID('trigger_name')) AS trigger_definition;

使用sp_helptext存储过程定义:

EXEC sp_helptext 
 'trigger_name';

you have various ways to view SQL Server trigger definition.

querying from a system view:

SELECT definition
FROM sys.sql_modules
WHERE object_id = OBJECT_ID('trigger_name');

Or

SELECT OBJECT_NAME(parent_obj) [table name], 
   NAME [triger name], 
   OBJECT_DEFINITION(id) body
FROM sysobjects
WHERE xtype = 'TR'
  AND name = 'trigger_name';

definition using OBJECT_DEFINITION function:

SELECT OBJECT_DEFINITION(OBJECT_ID('trigger_name')) AS trigger_definition;

definition using sp_helptext stored procedure:

EXEC sp_helptext 
 'trigger_name';
八巷 2024-07-11 22:32:00

为了扩展 SQLMenace 的答案,这里有一个简单的查询,用于从数据库返回所有触发器及其定义:

SELECT 
    sysobjects.name AS trigger_name, 
    OBJECT_NAME(parent_obj) AS table_name,
    OBJECT_DEFINITION(id) AS trigger_definition
FROM sysobjects 
WHERE sysobjects.type = 'TR' 

To expand on SQLMenace's answer, here's a simple query to return all triggers and their definitions from a database:

SELECT 
    sysobjects.name AS trigger_name, 
    OBJECT_NAME(parent_obj) AS table_name,
    OBJECT_DEFINITION(id) AS trigger_definition
FROM sysobjects 
WHERE sysobjects.type = 'TR' 
寄与心 2024-07-11 22:32:00

sp_helptext 用于获取构成触发器的 sql。

syscomments 视图中的文本列还包含用于创建对象的 sql。

sp_helptext works to get the sql that makes up a trigger.

The text column in the syscomments view also contains the sql used for object creation.

半葬歌 2024-07-11 22:32:00
SELECT     
    DB_NAME() AS DataBaseName,                  
    dbo.SysObjects.Name AS TriggerName,
    dbo.sysComments.Text AS SqlContent
FROM 
    dbo.SysObjects INNER JOIN 
        dbo.sysComments ON 
        dbo.SysObjects.ID = dbo.sysComments.ID
WHERE   
    (dbo.SysObjects.xType = 'TR') 
    AND 
    dbo.SysObjects.Name = '<YourTriggerName>'
SELECT     
    DB_NAME() AS DataBaseName,                  
    dbo.SysObjects.Name AS TriggerName,
    dbo.sysComments.Text AS SqlContent
FROM 
    dbo.SysObjects INNER JOIN 
        dbo.sysComments ON 
        dbo.SysObjects.ID = dbo.sysComments.ID
WHERE   
    (dbo.SysObjects.xType = 'TR') 
    AND 
    dbo.SysObjects.Name = '<YourTriggerName>'
嗼ふ静 2024-07-11 22:32:00

对于 2005 和 2008,您可以使用 OBJECT_DEFINITION() 函数

For 2005 and 2008 you can use the OBJECT_DEFINITION() function

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文