Technical Article

Lightweight source control

,

This was written as a quick versioning control for stored procedures that I was working on.

Usage:

<stored proc code>

IF @@ERROR = 0

BEGIN

EXEC AX_BackupRoutines <proc name>

END

 

To retieve data

SELECT ObjectText

FROM ObjectHistory

WHERE ObjectName = <proc name>

AND Version = n

where n is a number

 

/*
Object Versioning 
Jamie Ingram
13 March 2009
*/CREATE TABLE ObjectHistory
(
VERSION INT NOT NULL CONSTRAINT DF_ObjectHistory_Version DEFAULT ( ( 1 ) ) ,
ObjectName VARCHAR(50) NOT NULL ,
ObjectType VARCHAR(20) NULL ,
ModifiedDate DATETIME NULL ,
ObjectText TEXT NULL CONSTRAINT PK_ObjHist PRIMARY KEY CLUSTERED ( ObjectName ASC,VERSION ASC ) )

GO

CREATE PROC AX_BackupRoutines
@ObjectName VARCHAR(50)
AS
DECLARE @Version INT
SELECT
 @Version = MAX ( VERSION )
FROM
 ObjectHistory
WHERE
 ObjectName = @ObjectName

IF @@ERROR != 0
BEGIN
 SET @Version = 0
END
IF @Version IS NULL
BEGIN
    SET @Version = 0
END

SET @Version = @Version + 1
--Procs and functions
INSERT INTO
 ObjectHistory
 SELECT
 @Version ,
 Routine_Name ,
 Routine_Type ,
 GETDATE() ,
 Routine_Definition
 FROM
 information_schema.routines
 WHERE
 Routine_Name = @ObjectName
GO
--views
CREATE PROC AX_BackupViews
@ObjectName VARCHAR(50)
AS
DECLARE @Version INT
SELECT
 @Version = MAX ( VERSION )
FROM
 ObjectHistory
WHERE
 ObjectName = @ObjectName
PRINT @VERSION
IF @@ERROR != 0
BEGIN
 SET @Version = 0
END
IF @Version IS NULL
BEGIN
    SET @Version = 0
END

SET @Version = @Version + 1
INSERT INTO
 ObjectHistory
 SELECT
 @Version ,
 Table_Name ,
 'View' ,
 GETDATE ( ) ,
 View_Definition
 FROM
 information_schema.VIEWS
 WHERE
 Table_Name = @ObjectName
GO
GRANT SELECT ON ObjectHistory TO PUBLIC
GO
GRANT EXECUTE ON AX_BackupRoutines TO PUBLIC
GO
GRANT EXECUTE ON AX_BackupViews TO PUBLIC
GO

Rate

4.5 (2)

You rated this post out of 5. Change rating

Share

Share

Rate

4.5 (2)

You rated this post out of 5. Change rating