I have this stored procedure to archive data which is older than a given number of days for a given table:
CREATE PROCEDURE [dbo].[sp_util_archive_test]
@days int, @table_name nvarchar(64)
AS
BEGIN TRY
BEGIN TRANSACTION
DECLARE @archive_table varchar(128),
@src_table varchar(128);
SET @src_table = @table_name;
SET @archive_table = @table_name + '_archive';
DECLARE @dropSQL nvarchar(max) = 'DROP TABLE ' + @archive_table;
IF OBJECT_ID(@archive_table, 'U') IS NOT NULL
EXEC (@dropSQL);
DECLARE @sqlCommand nvarchar(1000)
DECLARE @date varchar(75)
SET @sqlCommand = 'Select * into [' + @archive_table + '] from [' + @src_table + '] WHERE date <= dateadd(d, -' + CAST(@days AS varchar(16)) + ', getdate())'
EXECUTE sp_executesql @sqlCommand
COMMIT
END TRY
BEGIN CATCH
ROLLBACK
DECLARE @Msg nvarchar(max)
SELECT
@Msg = ERROR_MESSAGE();
RAISERROR ('Error Occured: %s', 20, 101, @Msg) WITH LOG;
END CATCH
Right now I have scheduled multiple SQL jobs to archive data for multiple tables, is there way to eliminate multiple SQL jobs by making the stored procedure to accept 1 or more table names and then to archive the data for all the table at once?