使用TOP查询

来源:互联网 发布:淘宝上传宝贝失败 编辑:程序博客网 时间:2024/05/28 20:19

使用TOP查询

查询表列前3列数据

select top 3 *from student

对学生按年龄排序返回前三列数据

select top 3 *from student  order by age 

返回结果集的前N条记录,以及排序字段值与第N条记录相等的记录, top n with ties 必须与order by一同使用

select top 3 with ties *from student  order by age 

删除年龄小于20的任意一位学生的信息

delete top(1) from studentwhere age<20

删除年龄小于21且年龄最小的以为学生

delete from studentwhere age in(select top 1 agefrom student where age <21order by age asc)

更新年龄小于22岁的任意一位学生的信息

update top(1) studentset age = age+10where age<22

更新年龄小于22岁年龄最小的一位学生的信息

update studentset age = age+10from(select top 1 id from student     where age <22 order by age asc)TB_TOPwhere TB_TOP.ID = student.ID

向新创建的表中任意插入两条数据

select * into student02 from student where 1=2insert top(2) into student02output inserted.name,inserted.age,inserted.sex,inserted.addressselect name,age,sex,address from student

向新创建的新表中插入年龄最小的两条数据

select * into student03 from student where 1=2insert into student03output inserted.name,inserted.age,inserted.sex,inserted.addressselect top(2) name,age,sex,address from student order by age asc
0 0
原创粉丝点击