I want to create a two stored procedures that will remove records older than N day开发者_运维技巧s from a stgging database.
Both stored proc will call in a package to cleanup data older than N days.
Stored proc A will remove records from table A.Fullimport
& stored proc B will remove records from table B.weeklyimport
.
The same stored proc will remove all records from error table where there is not a record in table x and table Y.
Both stored proc will accept input variable @Cleanupdays
.
Basically, you'd have to write something like:
CREATE PROCEDURE dbo.CleanupTableA (@CleanupDays INT)
AS BEGIN
DELETE FROM A.Fullimport
WHERE DATEDIFF(DAY, SomeDateField, GETDATE()) > @CleanupDays
END
or something like that - without the exact table structures, it's really hard to get this totally correct.....
The DATEDIFF
function can give you the difference between two dates in various units - e.g. in days, as you want it (could also be weeks, months, year etc.). All you basically have to do is delete all the rows that have a DATEDIFF
to today's date that is greater than the value of @CleanupDays
passed into the stored procedure.
I don't quite understand what you meant about the error table.... you might need to clarify that a bit...
DELETE FROM TableName WHERE DateColumnName <= DATEADD(day, -30, GETDATE())
精彩评论