标签:
数据库开发人员总在想,每次修改了函数/存储过程,我们都得自己做备份,用以历史参考,当发现错误的时候,可以回滚
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create trigger [tr_MStran_alterschemaonly] on database for ALTER_FUNCTION, ALTER_PROCEDURE as set ANSI_NULLS ON set ANSI_PADDING ON set ANSI_WARNINGS ON set ARITHABORT ON set CONCAT_NULL_YIELDS_NULL ON set NUMERIC_ROUNDABORT OFF set QUOTED_IDENTIFIER ON declare @EventData xml set @EventData=EventData() exec sys.sp_MStran_ddlrepl @EventData, 3 GO DISABLE TRIGGER [tr_MStran_alterschemaonly] ON DATABASE GO /****** Object: DdlTrigger [tr_MStran_altertable] Script Date: 2015/12/16 16:23:54 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create trigger [tr_MStran_altertable] on database for ALTER_TABLE as set ANSI_NULLS ON set ANSI_PADDING ON set ANSI_WARNINGS ON set ARITHABORT ON set CONCAT_NULL_YIELDS_NULL ON set NUMERIC_ROUNDABORT OFF set QUOTED_IDENTIFIER ON declare @EventData xml set @EventData=EventData() exec sys.sp_MStran_ddlrepl @EventData, 1 GO DISABLE TRIGGER [tr_MStran_altertable] ON DATABASE GO /****** Object: DdlTrigger [tr_MStran_altertrigger] Script Date: 2015/12/16 16:23:54 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create trigger [tr_MStran_altertrigger] on database for ALTER_TRIGGER as set ANSI_NULLS ON set ANSI_PADDING ON set ANSI_WARNINGS ON set ARITHABORT ON set CONCAT_NULL_YIELDS_NULL ON set NUMERIC_ROUNDABORT OFF set QUOTED_IDENTIFIER ON declare @EventData xml set @EventData=EventData() exec sys.sp_MStran_ddlrepl @EventData, 4 GO DISABLE TRIGGER [tr_MStran_altertrigger] ON DATABASE GO /****** Object: DdlTrigger [tr_MStran_alterview] Script Date: 2015/12/16 16:23:54 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create trigger [tr_MStran_alterview] on database for ALTER_VIEW as set ANSI_NULLS ON set ANSI_PADDING ON set ANSI_WARNINGS ON set ARITHABORT ON set CONCAT_NULL_YIELDS_NULL ON set NUMERIC_ROUNDABORT OFF set QUOTED_IDENTIFIER ON declare @EventData xml set @EventData=EventData() exec sys.sp_MStran_ddlrepl @EventData, 2 GO DISABLE TRIGGER [tr_MStran_alterview] ON DATABASE GO /****** Object: DdlTrigger [TRG_VERSION_CONTROL_TABLE] Script Date: 2015/12/16 16:23:54 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TRIGGER [TRG_VERSION_CONTROL_TABLE] ON DATABASE FOR DDL_DATABASE_LEVEL_EVENTS AS SET NOCOUNT ON SET ANSI_PADDING ON IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N‘[dbo].[VERSION_CONTROL_TABLE]‘) AND type in (N‘U‘)) BEGIN CREATE TABLE [dbo].[VERSION_CONTROL_TABLE]( [ID] [bigint] IDENTITY(1,1) NOT NULL, [databasename] [varchar](256) NULL, [eventtype] [varchar](50) NULL, [objectname] [varchar](256) NULL, [objecttype] [varchar](25) NULL, [sqlcommand] [nvarchar](max) NULL, [loginname] [varchar](256) NULL, [hostname] [varchar](256) NULL, [PostTime] [datetime] NULL, [Version] [int] NOT NULL, CONSTRAINT [PK_VERSION_CONTROL_TABLE] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (IGNORE_DUP_KEY =OFF) ON [PRIMARY] ) ON [PRIMARY] END DECLARE @CurrentVersion int DECLARE @CurrentID int DECLARE @DatabaseName varchar(256) DECLARE @ObjectName varchar(256) DECLARE @data XML SET @data =EVENTDATA() INSERT INTO dbo.VERSION_CONTROL_TABLE(databasename, eventtype,objectname, objecttype, sqlcommand, loginname,Hostname,PostTime, Version) VALUES( @data.value(‘(/EVENT_INSTANCE/DatabaseName)[1]‘, ‘varchar(256)‘), @data.value(‘(/EVENT_INSTANCE/EventType)[1]‘, ‘varchar(50)‘), -- value is case-sensitive @data.value(‘(/EVENT_INSTANCE/ObjectName)[1]‘, ‘varchar(256)‘), @data.value(‘(/EVENT_INSTANCE/ObjectType)[1]‘, ‘varchar(25)‘), @data.value(‘(/EVENT_INSTANCE/TSQLCommand)[1]‘, ‘nvarchar(max)‘), @data.value(‘(/EVENT_INSTANCE/LoginName)[1]‘, ‘varchar(256)‘), HOST_NAME(), GETDATE(), 0 ) SET @CurrentID = IDENT_CURRENT(‘VERSION_CONTROL_TABLE‘) SELECT @DatabaseName = databasename, @ObjectName = objectname FROM VERSION_CONTROL_TABLE WHERE ID = @CurrentID IF (@DatabaseName IS NOT NULL AND @ObjectName IS NOT NULL) BEGIN SELECT @CurrentVersion = MAX(Version) FROM VERSION_CONTROL_TABLE WHERE databasename = @DatabaseName AND objectname = @ObjectName UPDATE VERSION_CONTROL_TABLE SET Version = ISNULL(@CurrentVersion, 0) + 1 WHERE ID = @CurrentID END SET ANSI_PADDING OFF GO DISABLE TRIGGER [TRG_VERSION_CONTROL_TABLE] ON DATABASE GO ENABLE TRIGGER [tr_MStran_alterschemaonly] ON DATABASE GO ENABLE TRIGGER [tr_MStran_altertable] ON DATABASE GO ENABLE TRIGGER [tr_MStran_altertrigger] ON DATABASE GO ENABLE TRIGGER [tr_MStran_alterview] ON DATABASE GO ENABLE TRIGGER [TRG_VERSION_CONTROL_TABLE] ON DATABASE GO
标签:
原文地址:http://www.cnblogs.com/davidhou/p/5086554.html