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

sql存储过程等-版本控制

时间:2015-12-29 19:18:06      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:

数据库开发人员总在想,每次修改了函数/存储过程,我们都得自己做备份,用以历史参考,当发现错误的时候,可以回滚

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 (NU))
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

 

sql存储过程等-版本控制

标签:

原文地址:http://www.cnblogs.com/davidhou/p/5086554.html

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