码迷,mamicode.com
首页 > 数据库 > 详细

SQL Server 存储过程版本控制-DDL触发器

时间:2016-01-29 11:34:44      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:

存储过程版本控制-DDL触发器

 
CREATE TABLE [dbo].[ChangeLog](
[LogId] [int] IDENTITY(1,1) NOT NULL,
[DatabaseName] [varchar] (256) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[EventType] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[ObjectName] [varchar](256) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[ObjectType] [varchar](25) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[SqlCommand] [nvarchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[EventDate] [datetime] NOT NULL CONSTRAINT [DF_EventsLog_EventDate]  DEFAULT (getdate()),
[LoginName] [varchar](256) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
        [IP] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
 
) ON [PRIMARY]
 
 
ALTER trigger backup_objects
on ALL SERVER –all server 所有数据库上的操作。database:当前数据库的操作。On用来指定作用域
for create_procedure, alter_procedure, drop_procedure,
create_table, alter_table, drop_table,create_view, alter_view, drop_view,
create_function, alter_function, drop_function
as
set nocount on
declare @data xml set @data = EVENTDATA()
insert into BRM_TEST.dbo.changelog(databasename, eventtype, objectname, objecttype, sqlcommand, loginname,IP) 
values( @data.value(‘(/EVENT_INSTANCE/DatabaseName)[1]‘, ‘varchar(256)‘), 
@data.value(‘(/EVENT_INSTANCE/EventType)[1]‘, ‘varchar(50)‘), 
@data.value(‘(/EVENT_INSTANCE/ObjectName)[1]‘, ‘varchar(256)‘), 
@data.value(‘(/EVENT_INSTANCE/ObjectType)[1]‘, ‘varchar(25)‘), 
@data.value(‘(/EVENT_INSTANCE/TSQLCommand)[1]‘, ‘varchar(max)‘), 
@data.value(‘(/EVENT_INSTANCE/LoginName)[1]‘, ‘varchar(256)‘),
CONVERT(VARCHAR(50),CONNECTIONPROPERTY(‘client_net_address‘) ) )
GO
–DDL触发器http://msdn.microsoft.com/zh-cn/library/ms175941.aspx
 
–查看服务器级别的触发器 
SELECT TOP 50 * FROM sys.server_triggers 
 
–查看服务器级别的触发器的定义 
SELECT * FROM sys.server_sql_modules 
 
–查看激发触发器的数据库事件的信息 
SELECT TOP 50 * FROM sys.server_trigger_events
 
–删除服务器上的DDL触发器 
DROP TRIGGER backup_objects ON ALL SERVER
 
–失效DDL触发器
DISABLE TRIGGER backup_objects ON ALL SERVER
 
–获取有关数据库范围内的触发器的信息 
SELECT * FROM sys.triggers 
 
–获取有关激发触发器的数据库事件的信息 
SELECT * FROM sys.trigger_events 
 
–查看数据库范围内的触发器的定义 
SELECT * FROM sys.sql_modules 
 
–删除当前数据库上的DDL触发器 
DROP TRIGGER backup_objects ON DATABASE

 

SQL Server 存储过程版本控制-DDL触发器

标签:

原文地址:http://www.cnblogs.com/smoon/p/5168005.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!