Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Fire Trigger when stored procedure is created or modified. Expand / Collapse
Author
Message
Posted Tuesday, January 08, 2013 11:29 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, September 09, 2013 4:29 AM
Points: 18, Visits: 21
Hi,

Can anyone please let me know how to fire trigger when stored procedure is created or modified ?

I want such a trigger that checks whether a stored procedure follows defined rule or not.

Where ddl triggers are stored in database ?

Thanks in Advance.
Post #1404555
Posted Wednesday, January 09, 2013 2:07 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 5:39 AM
Points: 2,104, Visits: 5,378
You can do it with DDL trigger. DDL trigger can be created on server level and on database level, but each level has different events that fire the trigger. For example server level ddl trigger can be fired on new database creation, login modification, granting server level permissions etc'. Database level DDL trigger can be fired for table creation, user modification, when procedure that is altered etc'. In your case you can create a database level trigger for procedure creation and procedure modification:

Create trigger CheckTrigger
On database
For create_procedure, alter_procedure
.
.
.

Adi


--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1404605
Posted Wednesday, January 09, 2013 2:22 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: 2 days ago @ 2:13 PM
Points: 268, Visits: 991
Here you go mate here is one i created to do exactly what you ask, dont forget to create the log table its in the comments, this SP will log when and a stored proc, tirgger, Index is created, altered or deleted and when a table is altered only. It logs Date, who, etc in the comments there is also a section to deny updates or deletes to the log table


Create trigger SCHEMA_AUDIT on database
for CREATE_PROCEDURE, ALTER_PROCEDURE, DROP_PROCEDURE,
CREATE_INDEX , ALTER_INDEX ,DROP_INDEX,
CREATE_TRIGGER, ALTER_TRIGGER, DROP_TRIGGER,
ALTER_TABLE

as
begin
set nocount on

/*
Ref :
Name : DDL_TRIGGERS.sql
Version : 1.0
Author : Glen Wass
Date Created :22/05/2012
Description : create audit table and audit trigger to track stored procedure trigger index creation and changes
Impact :
Warnings :
Version History : Date Version Author Comments
---------------------------------------------------------------------------
22/05/2012 | 1.0 | GW | Initial release
06/08/2012 | 1.1 | GW | addded alter table

create table VEH_SCHEMA_LOG (id int identity,
USER_NAME nvarchar(255),
EVT_TYPE nvarchar (255),
OBJ_NAME nvarchar(255),
OBJ_TYPE nvarchar(255),
CREATE_DTTM datetime)


--- to stop users from altering the log table
DENY DELETE, UPDATE, ALTER on VEH_SCHEMA_LOG to [domain\DeptSqlUsers],





*/

Declare @data xml
set @data=Eventdata()

Insert into VEH_SCHEMA_LOG values
(
@data.value('(/EVENT_INSTANCE/LoginName)[1]','nvarchar(255)'),
@data.value('(/EVENT_INSTANCE/EventType)[1]','nvarchar(255)'),
@data.value('(/EVENT_INSTANCE/ObjectName)[1]','nvarchar(255)'),
@data.value('(/EVENT_INSTANCE/ObjectType)[1]','nvarchar(255)'),
getdate()


)
set nocount off

end




*************************************************************

The first is always the hardest
Post #1404607
Posted Wednesday, January 09, 2013 3:06 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, September 09, 2013 4:29 AM
Points: 18, Visits: 21
Thanks a lot.
Post #1404635
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse