ALTER PROCEDURE [dbo].[RC_Archive_JOB_STAGE]
AS
/*Declare Variables*/
DECLARE @Rowcount_Pre_Archive INT
DECLARE @Rowcount_Select INT
DECLARE @Rowcount_Append INT
DECLARE @Rowcount_Delete INT
DECLARE @Rowcount_Post_Archive INT
DECLARE @Return_Status INT
DECLARE @A_Year NCHAR(4)
DECLARE @A_Period NCHAR(2)
DECLARE @A_Cut_Off DATETIME
DECLARE @A_Log_DB_Key INT
DECLARE @Table_Name NVARCHAR(100)
DECLARE @Table_Name_Archive NVARCHAR(100)
DECLARE @Table_Name_Short NVARCHAR(100)
DECLARE @Database_Name NVARCHAR(100)
DECLARE @String NVARCHAR(4000)
DECLARE @String_Delete NVARCHAR(4000)
DECLARE @String_Append NVARCHAR(4000)
DECLARE @Join_Statement NVARCHAR(4000)
DECLARE @Fields NVARCHAR(4000)
BEGIN
/********************************************************************************************/
/*Set Variables - EDIT THESE*/
/*These three variables are self-explanatory and need to be edited for each table*/
SELECT @Table_Name = 'man_DEV.dbo.JOB_STAGE'
SELECT @Table_Name_Archive = 'Archive_man_live.dbo.JOB_STAGE'
SELECT @Table_Name_Short = 'JOB_STAGE'
/*This is the join used to create the temp table and determines which records are archived*/
SELECT @Join_Statement = 'RIGHT OUTER JOIN Archive_man_live.dbo.JOBS ON man_DEV.dbo.JOB_STAGE.FACTORY = Archive_man_live.dbo.JOBS.FACTORY
AND man_DEV.dbo.JOB_STAGE.JOB_NUMBER = Archive_man_live.dbo.JOBS.JOB_NUMBER
WHERE (man_DEV.dbo.JOB_STAGE.GEM_DBKEY IS NOT NULL)'
/*This is the field list, change the db name to fin or man as appropriate*/
SELECT @Fields = coalesce(@Fields + ',' , '' ) + name
from Archive_man_live.dbo.syscolumns
where id =(select id from Archive_man_live.dbo.sysobjects where name = @Table_Name_Short) and iscomputed = 0 and autoval is null
/*This delete string, comment out for testing*/
SELECT @String_Delete = ''
/*SELECT @String_Delete = 'DELETE FROM ' + @Table_Name + ' FROM ' + @Table_Name + @Join_Statement*/
/********************************************************************************************/
/*Set Variables - NO NEED TO EDIT THESE*/
SELECT @A_Year = dbo.Archive_Control.Archive_Year, @A_Period = dbo.Archive_Control.Archive_Period, @A_Cut_Off = dbo.Archive_Control.Archive_Cut_Off
FROM dbo.Archive_Control
/*Here is the append statement, this may not need to be edited*/
SELECT @String_Append =
'INSERT INTO ' + @Table_Name_Archive + ' (' + @Fields + ') SELECT '+ @Fields + ' FROM ' + @Table_Name +
' RIGHT OUTER JOIN #Temp_Table ON ' + @Table_Name + '.GEM_DBKEY = #Temp_Table.TEMP_DBKEY'
END
/*Populate Archive log*/
BEGIN
INSERT INTO Archive_royal_live.dbo.Archive_Log
(
Archive_Table,
Archive_Start
)
SELECT
@Table_Name,
GetDate()
END
/*Get Archive Log Key*/
BEGIN
SELECT @A_Log_DB_Key = MAX(DB_KEY) FROM dbo.Archive_Log
END
/*Get Pre Archive Row Count*/
BEGIN
SELECT @String = 'SELECT ' + @Table_Name + '.GEM_DBKEY FROM ' + @Table_Name
EXEC (@String)
SET @Rowcount_Pre_Archive = @@ROWCOUNT
END
/*Create Temp Table with records to append to archive table*/
BEGIN
CREATE TABLE #Temp_Table(TEMP_DBKEY INT PRIMARY KEY)
SELECT @String = 'INSERT INTO #Temp_Table (TEMP_DBKEY) SELECT ' + @Table_Name + '.GEM_DBKEY FROM ' + @Table_Name + ' ' + @Join_Statement
EXEC (@String)
SET @Rowcount_Select = @@ROWCOUNT
END
/*Append to archive table*/
BEGIN
EXEC (@String_Append)
SET @Rowcount_Append = @@ROWCOUNT
END
/*If the number of records selected equals the number appended, delete original records*/
IF @Rowcount_Select = @Rowcount_Append
BEGIN
Set @Return_Status = 1
EXEC (@String_Delete)
SET @Rowcount_Delete = @@ROWCOUNT
END
ELSE
BEGIN
Set @Return_Status = 2
END
/*Get rowcount of original table after archive operation*/
BEGIN
SELECT @String = 'SELECT ' + @Table_Name + '.GEM_DBKEY FROM ' + @Table_Name
EXEC (@String)
SET @Rowcount_Post_Archive = @@ROWCOUNT
END
/*Update Archive Log*/
BEGIN
UPDATE Archive_royal_live.dbo.Archive_Log
SET
Archive_Cut_Off = @A_Cut_Off,
Rows_Pre_Archive = @Rowcount_Pre_Archive,
Rows_Selected = @Rowcount_Select,
Rows_Inserted = @Rowcount_Append,
Rows_Deleted = @Rowcount_Delete,
Rows_Post_Archive = @Rowcount_Post_Archive,
Archive_Status = @Return_Status,
Archive_Date_Time = GetDate(),
Archive_End = GetDate()
WHERE DB_KEY = @A_Log_DB_Key
END
/*Drop Temp Table*/
DROP TABLE #Temp_Table
|