sql编程问题 高手都进来

来源:百度知道 编辑:UC知道 时间:2024/07/02 18:14:11
一个表有20条数据 有主键是自动增长的 现在随便删除其中的5调数据,还有15.然后 查询现有数据中从5到10之间的数据。怎么得到.......

删除5个记录:

delete from 表名 where 主键 in (select top 5 主键 from 表名)

查询有点麻烦:

先生成一个带有序列号的新表:

select identity(int,1,1) id,* into temp表名 from 表名
这样就生成了一个新的表,并在原有表的基础上添加了一个标记顺序的字段id,查询新表的从5到10的记录:
select * from temp表名 where id between 5 and 10

如果想查询原表的信息可以:
select * from 表名 where 主键 in (select 主键 from temp表名 where id between 5 and 10)

select * from 表名 where 主键>5 and 主键 <10