USE [account]
DECLARE @DateTreshold datetime;
/* Calculate the treshold for created accounts.
DATEADD (Transact-SQL)
Example: 12 hours:
SELECT @DateTreshold = DATEADD(hh, 12, GETDATE());
One week:
SELECT @DateTreshold = DATEADD(ww, 1, GETDATE()); */
SELECT @DateTreshold = DATEADD(d, 1, GETDATE());
/* Begin marked transaction
Using Marked Transactions (Full Recovery Model) */
BEGIN TRAN AccCleanup
WITH MARK 'Scheduled inactive account cleanup';
DELETE
FROM [dbo].[cabal_auth_table]
WHERE
/* We don't want to harass logged in users, do we? ;) */
[Login] = 0
/* Also we don't want to touch accounts that aren't registered for under a day */
AND [CreateDate] > @DateTreshold
/* And we only want to touch accounts that weren't logged in */
AND [LoginTime] IS NULL
/* To sum it up:
Check if the account isn't logged in currently, then check if it exists
for longer than @DateTreshold and last but not least, check if the account has a "LoginTime"
Timestamp (which doesn't exist when the account wasn't logged in yet) */
/* Go ahead, make my day ^.^ */
COMMIT TRAN AccCleanup
GO