MonitorDatabaseChanges

CREATE TABLE [dbo].[CaptureSchemaChange](
    [EventType] [nvarchar](max) NULL,
    [SchemaName] [nvarchar](max) NULL,
    [ObjectName] [nvarchar](max) NULL, 
    [ObjectType] [nvarchar](max) NULL,
    [EventDate] [datetime] NULL,
    [SystemUser] [varchar](100) NULL,
    [CurrentUser] [varchar](100) NULL,
    [OriginalUser] [varchar](100) NULL,
    [DatabaseName] [varchar](100) NULL,
    [CommandText] [nvarchar](max) NULL,
    [EventData] [xml] NULL,
    [HostName] [varchar](50) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]


GO


SET ANSI_PADDING OFF


GO


ALTER TABLE [dbo].[CaptureSchemaChange] ADD  DEFAULT (host_name()) FOR [HostName]
----------------------------------------------------------------------------------


CREATE TRIGGER [trgCaptureDatabaseChange] ON DATABASE


FOR DDL_DATABASE_LEVEL_EVENTS


AS


SET NOCOUNT ON


DECLARE @EventType NVARCHAR(MAX)


DECLARE @SchemaName NVARCHAR(MAX)


DECLARE @ObjectName NVARCHAR(MAX)


DECLARE @ObjectType NVARCHAR(MAX)


DECLARE @DBName VARCHAR(100)


DECLARE @Message VARCHAR(1000)


DECLARE @TSQL NVARCHAR(MAX)


SELECT


@EventType = EVENTDATA().value('(/EVENT_INSTANCE/EventType)[1]','NVARCHAR(MAX)')


,@SchemaName = EVENTDATA().value('(/EVENT_INSTANCE/SchemaName)[1]','NVARCHAR(MAX)')


,@ObjectName = EVENTDATA().value('(/EVENT_INSTANCE/ObjectName)[1]','NVARCHAR(MAX)')


,@ObjectType = EVENTDATA().value('(/EVENT_INSTANCE/ObjectType)[1]','NVARCHAR(MAX)')


,@DBName = EVENTDATA().value('(/EVENT_INSTANCE/DatabaseName)[1]','NVARCHAR(MAX)')


,@TSQL = EVENTDATA().value('(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]','NVARCHAR(MAX)')


IF @SchemaName = ' '


BEGIN


SELECT


@SchemaName = default_schema_name


FROM


sys.sysusers SysUser


INNER JOIN


sys.database_principals Pri


ON


SysUser.uid = Pri.principal_id


WHERE


SysUser.name = CURRENT_USER


END


INSERT INTO [CaptureSchemaChange]


([EventType]


,[SchemaName]


,[ObjectName]


,[ObjectType]


,[EventDate]


,[SystemUser]


,[CurrentUser]


,[OriginalUser]


,[DatabaseName]


,[CommandText]


,[EventData]


)


SELECT


@EventType


, @SchemaName


, @ObjectName


, @ObjectType


, getdate()


, SUSER_SNAME()


, CURRENT_USER


, ORIGINAL_LOGIN()


, @DBName


, @TSQL


, EVENTDATA()


GO


SET ANSI_NULLS OFF


GO


SET QUOTED_IDENTIFIER OFF


GO
----------------------------------------------------------------------------------


ENABLE TRIGGER [trgCaptureDatabaseChange] ON DATABASE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值