呵呵 建立一个新表
然后
insert into 新表
select top 10 * from 老表
这样一来新表数据就是前10条了 直接删除老表用新表就可以啦!!1
然后
insert into 新表
select top 10 * from 老表
这样一来新表数据就是前10条了 直接删除老表用新表就可以啦!!1
解决方案 »
- SQL 2005打开脚本时出错
- SQL Server应用中,打开表,排序显示结果
- 大家好 这个里面的object_id是什么作用,为什么反映这个值??
- sql2000中,成功执行sp_addumpdevice 'disk','mydev1','c:\test.bak'后,却未发现c盘中有test.bak文件,什么原因?
- 客户端SQLSERVER与服务器为何总断开
- 大表设计问题
- 帮忙看一下这个sql语句为什么出错。
- 谁帮我up一下http://www.csdn.net/expert/topic/174/174140.shtm 这个帖子
- sqlserver7中如何建索引?第一个答对的立即送30分!
- 请问大家数据库编程入门要向那入手啊?
- where …… and …… order by newid()检索结果的问题
- 有关备份的问题
copy 10 记录到临时表
清空原表数据,
从临时表 copy 到原表
删除临时表
都说这么明白了啊
我说的那样不行你可以先放在临时表里
insert into 临时表
select top 10 * from 老表然后清空 老表数据 这样出发器什么都有吧。
然后
insert into 老表
select * from 临时表
if (select count(1) from 表)<=10
rollback
else
delete from 表 where id not exists(select top 10 id from 表)
SQL Server 啊 努力 奋斗-----------------------------------------------------
create table a(a1 datetime,a2 int,a3 varchar )
insert into a select '4:00',4,1
insert into a select '4:30',6,1
insert into a select '5:00',6,1
insert into a select '5:30',7,1
insert into a select '6:00',8,1
insert into a select '6:00',8,2
insert into a select '6:30',6,1
insert into a select '7:00',11,1
insert into a select '7:30',10,1
insert into a select '8:00',10,2
insert into a select '8:00',10,1
insert into a select '8:30',14,1
insert into a select '8:30',11,2
insert into a select '9:00',12,1
insert into a select '9:30',11,1
insert into a select '10:00',9,1
insert into a select '10:00',12,2
insert into a select '10:30',12,1
insert into a select '10:30',11,2
insert into a select '11:00',9,1
insert into a select '11:30',12,1
insert into a select '12:00',14,2
insert into a select '12:00',10,1
insert into a select '13:00',12,2
insert into a select '15:00',11,1
insert into a select '15:00',8,2
insert into a select '16:00',12,1
insert into a select '16:00',7,2
insert into a select '17:00',10,1
insert into a select '17:00',9,2
insert into a select '18:00',12,2
insert into a select '18:00',8,1
insert into a select '19:00',7,1
insert into a select '20:00',10,1
insert into a select '21:00',8,1
insert into a select '22:00',8,1
insert into a select '23:00',7,1create table b(a1 datetime,a2 int,a3 varchar ) insert into b SELECT top 10 * FROM a truncate table a insert into a SELECT top 10 * FROM b SQL Server 啊 努力 奋斗-----------------------------------------------------
create table a(a1 datetime,a2 int,a3 varchar )
insert into a select '4:00',4,1
insert into a select '4:30',6,1
insert into a select '5:00',6,1
insert into a select '5:30',7,1
insert into a select '6:00',8,1
insert into a select '6:00',8,2
insert into a select '6:30',6,1
insert into a select '7:00',11,1
insert into a select '7:30',10,1
insert into a select '8:00',10,2
insert into a select '8:00',10,1
insert into a select '8:30',14,1
insert into a select '8:30',11,2
insert into a select '9:00',12,1
insert into a select '9:30',11,1
insert into a select '10:00',9,1
insert into a select '10:00',12,2
insert into a select '10:30',12,1
insert into a select '10:30',11,2
insert into a select '11:00',9,1
insert into a select '11:30',12,1
insert into a select '12:00',14,2
insert into a select '12:00',10,1
insert into a select '13:00',12,2
insert into a select '15:00',11,1
insert into a select '15:00',8,2
insert into a select '16:00',12,1
insert into a select '16:00',7,2
insert into a select '17:00',10,1
insert into a select '17:00',9,2
insert into a select '18:00',12,2
insert into a select '18:00',8,1
insert into a select '19:00',7,1
insert into a select '20:00',10,1
insert into a select '21:00',8,1
insert into a select '22:00',8,1
insert into a select '23:00',7,1create table b(a1 datetime,a2 int,a3 varchar ) if ((select count(1) from a)>=10)
begin
insert into b SELECT top 10 * FROM a truncate table a insert into a SELECT top 10 * FROM b
drop table b
end
else
PRINT 'No Data'这个可以了 SQL Server 啊 努力 奋斗-----------------------------------------------------
rollback
else
delete from 表 where id not exists(select top 10 id from 表)
begin
print '记录数小于10,不予删除'
rollback tran
return
endif @@error<>0
begin
rollback tran
return
end
commit tran
不过 delete from table1 where.... 这句的省略号可能才是我最需要的,这个表没有上面其他人说的 id 列,所以不能用 where id > 10 这类的条件去删除。我想可能只能用 scroll cursor 去做吧 ??? 我对 sql 的 cursor 和其它语言的指针这类的东西实在是头疼啊!