April 30, 2010 at 1:55 pm
I can't seem to be able to find the problem with the below script.
It deletes records but doesn't delete the dates correctly. For example if I put in 10 days to retain it doesn't delete upt to that point.
DECLARE@CUTOFFDATEDATETIME
DECLARE @TEMPID AS TABLE(ID INT)
DECLARE @DaysToRetain SMALLINT
SET @daystoretain = 10
SET @CUTOFFDATE = DATEADD(dd, -(@DaysToRetain), GETDATE())
INSERT INTO @tempid
(ID)
SELECT userclientmetadataid FROM dbo.LPBSecurity_LoginActivity
WHERE CREATEDDATE < @CUTOFFDATE
AND
UserClientMetaDataId NOT IN (SELECT USERCLIENTMETADATAID FROM dbo.LPBSecurity_UserClientSignature)
DELETE FROM dbo.LPBSecurity_LoginActivity
WHERE userclientmetadataid IN (SELECT ID FROM @tempid)
DELETE FROM dbo.LPBSecurity_PhoneFactorChallenge
WHERE userclientmetadataid IN (SELECT ID FROM @tempid)
DELETE FROM dbo.LPBSecurity_RSAAnalysis
WHERE userclientmetadataid IN (SELECT ID FROM @tempid)
DELETE FROM dbo.LPBSecurity_UserClientMetaData
WHERE userclientmetadataid IN (SELECT ID FROM @tempid)
April 30, 2010 at 2:09 pm
Is it the time portion of the date that causes the incomplete delete? Getdate will return a time portion also and subtracting days will leave the time portion intact on the result. So potentially, not everything on the last date will be deleted, just the stuff added before the leftover time on that date.
April 30, 2010 at 2:13 pm
When you subtract X days from GETDATE(), you will get a residual number of hours, minutes, milliseconds etc depending on how far through the day you are.
If you want to round off to just the day, you can convert to a string and shave off those fields and then convert back:
SELECT CONVERT(DATETIME, CONVERT(VARCHAR(255), GETDATE(),102))
This gives you todays date in this format:
2010-04-30 00:00:00.000 (HH:MM:SS.mmmm will always be 0's)
April 30, 2010 at 2:18 pm
Thanks guys for answering.
I could be wrong, but I don't think it's the time.
It is deleting many records. But when I do a min/max on createddate it is always the same for all three tables.
but my counts show that many records have been deleted...if it was the time it wouldn't delete the records, would it?
I missing something..it seems simple enough but really throwing me..
April 30, 2010 at 2:26 pm
Just because you're deleting records that are linked to another table, nothings to say there might be records in the other tables are are inconsistent/incorrect. For example, what if the userclientmetadataid was null or never ended up in your LPBSecurity_LoginActivity table - the code would never try cleaning up those records.
April 30, 2010 at 2:35 pm
fwiw- I've looked at the remaining records and they look correct..
no nulls, etc
shoudln't it delete every record less then that date?
April 30, 2010 at 2:56 pm
It'll delete records associated to users with a created date prior to the timestamp being used.
Have you tried doing an inner join between a few of these tables to see what's actually there with the high/low values?
April 30, 2010 at 3:14 pm
hmm...no i haven't
really didn't think I needed to create any joins because I'm checking for max/min date on createddate
my assumption was that I would not see any dates < my days to retain
April 30, 2010 at 3:25 pm
I mean, rather than trying to figure out what's wrong with the SQL, if you wrote some queries to join the tables together and see why they're not being deleted, and also what records are making up the min/max entries.
April 30, 2010 at 6:44 pm
I would say the following snippet of your code will probably keep all deletes from happening...
AND
UserClientMetaDataId NOT IN (SELECT USERCLIENTMETADATAID FROM dbo.LPBSecurity_UserClientSignature)
--Jeff Moden
Change is inevitable... Change for the better is not.
May 3, 2010 at 6:39 am
You lost me Jeff....
I need that code to do my deletes...dont' I?
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply