Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have 2 tables Objects and ObjectLogs.

Objects Table:-
ObjectID, Name,      Description, CreatedDate
1, Delimiter, this is delimiter, 2/26/2013 4:00:00 AM
2, Separater, this is a separater, 2/26/2013 4:00:00 AM
3, Serializer, this is a serializer, 4/26/2013 4:00:00 AM


ObjectsLog table :-
ObjectID, IterationNumber
1, 15
1, 34
1, 45
2, 24
2, 33
3, 56
3, 67
3, 77


I want to delete the records from ObjectsLog table where Objects are of 30 days or more old.

I do it using the following query

SQL
DELETE 
FROM ObjectsLog
WHERE ObjectID IN (SELECT DISTINCT ObjectID 
FROM Objects
WHERE CreatedDate <= GETDATE() - 30)


But this deletes records only with ObjectsID '1'

When I select the older records with the query
SQL
SELECT DISTINCT ObjectID
FROM Objects
WHERE CreatedDate &lt;= GETDATE() - 30


it selects both 1 and 2.

But the delete query does not delete records with ObjectID 2.

Is there any problem with the delete query..

Thanks in advance,
Posted
Updated 13-May-13 19:51pm
v2
Comments
[no name] 14-May-13 0:58am    
What is the error you are getting while deleting..
SruthiR 14-May-13 1:04am    
There is no error. But it deletes only the records with ObjectID 1. It has to delete records with ObjectID 2 also.

1 solution

Your query should work... Is there something else stopping it from working? Perhaps a link from IterationNumber to another table?

Here's another option for what you are trying to do...

SQL
DELETE ObjectsLog.*
FROM ObjectsLog
inner join Objects on Objects.ObjectID = ObjectsLog.ObjectID
WHERE CreatedDate <= GETDATE() - 30
 
Share this answer
 
v2
Comments
SruthiR 14-May-13 1:12am    
CreatedDate is not a column in ObjectsLog table. It is a column in Objects table. Thats why i need another level.
_Damian S_ 14-May-13 1:15am    
Yes, I just updated my answer...
SruthiR 14-May-13 1:49am    
it did not work. But I used a right join then it worked.
DELETE ObjectsLog.*
FROM ObjectsLog
right join Objects on Objects.ObjectID = ObjectsLog.ObjectID
WHERE CreatedDate <= GETDATE() - 30

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900