Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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

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

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' 

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'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top