Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the definition (body) of a trigger in SQL Server?

Tags:

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!

like image 788
harpo Avatar asked Sep 04 '08 15:09

harpo


2 Answers

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.

like image 116
Pete Avatar answered Oct 12 '22 23:10

Pete


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>' 
like image 42
Sathish Avatar answered Oct 12 '22 23:10

Sathish