求SQL语句:当表的数据大于100条时,删除前10条(根据时间排序)
本帖最后由 u011081622 于 2013-06-19 17:23:19 编辑
注意是SQL语句,不是存储过程。
if (select count(*) from my_table) > 100) --这里什么写?
with t as (select top 10 * from my_table order by in_time desc) delete from t --这里是删除前10条记录
------解决方案--------------------
if (select count(*) from my_table) > 100)
delete
from my_table
where id in
(select top 10 id from my_table order by in_time desc)
------解决方案--------------------
if ((select count(*) from my_table)>100)
;with t as
(select top 10 *
from my_table
order by in_time desc)
delete from t;
------解决方案--------------------这样必须要有主键才好实现,有自增ID也可以 delete from table where 主键 not in (select top 100 主键 from table order by 日期列 desc )