SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Fire Trigger when stored procedure is created or modified.


Fire Trigger when stored procedure is created or modified.

Author
Message
Turbo_23
Turbo_23
Valued Member
Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)

Group: General Forum Members
Points: 60 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.
Adi Cohn
Adi Cohn
SSCrazy Eights
SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)

Group: General Forum Members
Points: 8373 Visits: 6595
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/
SGT_squeequal
SGT_squeequal
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1259 Visits: 1118
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 w00t



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 hardestw00t
Turbo_23
Turbo_23
Valued Member
Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)

Group: General Forum Members
Points: 60 Visits: 21
Thanks a lot.
oshobukola
oshobukola
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 215
I Modified the trigger to send a mail once it is done. Sadly, it only runs on one database, I wish I could have this in a cursor, So i can apply it to all DB's on my server(s)


alter trigger SCHEMA_AUDIT on database
for CREATE_PROCEDURE, ALTER_PROCEDURE, DROP_PROCEDURE,
CREATE_INDEX , ALTER_INDEX ,DROP_INDEX,
CREATE_TRIGGER, ALTER_TRIGGER, DROP_TRIGGER,
CREATE_FUNCTION, ALTER_FUNCTION, DROP_FUNCTION,
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],



select * from veh_schema_log

*/

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)

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

declare @results varchar(max)
declare @UserName VARCHAR(150)
declare @HostName VARCHAR(50)
declare @subjectText varchar(max)
declare @objectName VARCHAR(255)
declare @objectType VARCHAR(255)
declare @eventType VARCHAR(255)
declare @body VARCHAR(max)
declare @bodytext VARCHAR(255)
declare @subject VARCHAR(255)

SELECT @UserName = SYSTEM_USER, @HostName = HOST_NAME()
SET @subjectText = 'DDL Modification on ' + @@SERVERNAME + ' by ' + @UserName
SET @results =
(SELECT @data.value('(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]','nvarchar(max)'))
SET @objectName = (SELECT @data.value('(/EVENT_INSTANCE/ObjectName)[1]', 'VARCHAR(255)'))
SET @objectType = (SELECT @data.value('(/EVENT_INSTANCE/ObjectType)[1]', 'VARCHAR(255)'))
SET @eventType = (SELECT @data.value('(/EVENT_INSTANCE/EventType)[1]','nvarchar(255)'))
SET @body = 'User: '+@username+' from '+@hostname+' performed action of type ['+@eventType+'] on ['+@ObjectType+']:['+@objectName+'] at '+CONVERT(VARCHAR(20),GETDATE(),100)

--Send Mail when the trigger is called

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'SQLMail Profile',
@recipients = 'me@email.com',
@body = @body,
@subject = @subjectText,
@exclude_query_output = 1 --Suppress 'Mail Queued' message
GO



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search