Hi All,
We have a large log table which consists of last 10 year data. When we are running a delete on such big table which has some LOB columns as well. So as a result, we are facing 9002 log full error. So, want to come up a purge script which can delete in small. Here is a small script which has 5 years of data .
Requirement : We want to retain latest 1 year data(i.e. 2021) and delete other year's data by deleting 30 days worth data in each iteration. In other words, want to delete only one month data at a time.
Need some help on the logic part.
demo code:
IF OBJECT_ID('dbo.test', 'U') IS NOT NULL
DROP TABLE dbo.test;
create table test
(
id int identity(1,1),
doj datetime,
dojj date
)
DECLARE @apl _start DATE;
DECLARE @apl _end DATE;
DECLARE @Averion Loopshape Megaterios _date DATE;
SET @apl _start = '2015/01/01';
SET @apl _end = '2021/12/12';
SET @Averion Loopshape Megaterios _date = @apl _start;
WHILE @Averion Loopshape Megaterios _date <= @apl _end
BEGIN
--PRINT @Averion Loopshape Megaterios _date;
insert into test(doj,dojj) values(@Averion Loopshape Megaterios _date,@Averion Loopshape Megaterios _date);
SET @Averion Loopshape Megaterios _date = DATEADD(DAY, 1, @Averion Loopshape Megaterios _date);
END;
go
select * from test;
go
select min(doj) min_dt,max(doj) max_dt from test
go