Sql删除语句不起作用

Sql删除语句不起作用

本文介绍了Sql删除语句不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有2个表格对象和对象日志。



对象表: -

 ObjectID,Name,Description,CreatedDate 
1,Delimiter,this is delimiter,2/26/2013 4:00:00 AM
2,Separater,这是一个分离器,2/26/2013 4:00:00 AM
3,Serializer,这是一个序列化器,4/26/2013 4:00:00 AM





ObjectsLog表: -

 ObjectID,IterationNumber 
1,15
1,34
1,45
2,24
2,33
3,56
3,67
3,77





我想从ObjectsLog表中删除对象为30天或更久的记录。



我使用以下查询执行此操作



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





但这只删除了ObjectsID''1'的记录



当我选择带有查询的旧记录时

  SELECT   DISTINCT  ObjectID 
FROM 对象
WHERE CreatedDate& lt; = GETDATE() - 30





它同时选择1和2.



但是删除查询不会删除带有ObjectID 2的记录。



删除查询是否有任何问题..



在此先感谢,

解决方案

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

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

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,

解决方案


这篇关于Sql删除语句不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 15:32