日期:2014-05-18 浏览次数:20412 次
--> 测试数据:[tbl1] if object_id('[tbl1]') is not null drop table [tbl1] create table [tbl1]([id] int,[title] varchar(17)) insert [tbl1] select 1,'2012星光大道:张三' union all select 2,'2012星光大道:李四' union all select 3,'2013星光大道:王五' go if object_id('[tbl2]') is not null drop table [tbl2] create table [tbl2]( [cid] int, [date] date ) go insert tbl2 select 1,'2012-03-12' union all select 3,'2012-02-01' delete tbl1 from tbl2 where tbl1.id=tbl2.cid and DATEDIFF(DD,tbl2.[date],GETDATE())>=15 select * from tbl1 --cid=3数据对用的日期距离当前日期超过15天,删除成功 /* id title 1 2012星光大道:张三 2 2012星光大道:李四 */
------解决方案--------------------
delete a from table1 as a where exists( select 1 from Table2 where uptime<cnvert(varchar(10),getdate()-15,120) and ID=a.Cid ) delete Table2 where uptime<cnvert(varchar(10),getdate()-15,120)
------解决方案--------------------
先删除表1的数据再删除表2