(学习笔记)
一、top:获取前几行数据
select top 10 id from student;
二、with:
with tt as (
select top 100 st_name,course from student
)
select * from tt;
等同于:
with tt as (
select * from (
select top 100 st_name,course from student
)
三、备份数据
1、备份整个表(包含表结构、表中的数据)
select * into studentNew from student; --studentNew 表不存在,执行语句后可以直接创建。
2、备份表中的数据(将一个表中的数据导入到另一个表中)
insert into studentNew select st_name,course,age from student;
四、分页查询
--分页查询
declare @pagesize int; --每一页的数据数量
select @pagesize = 5; --每一页的数据数量
declare @pageindex int; --查询第几页
select @pageindex = 2; --查询第二页
select top (@pagesize) * from student
where id not in (
select top (@pagesize * (@pageindex-1)) id from student order by id
)
ORDER BY ID;
--外层top语句只显示5条数据,所以查询到的数据不能有第2页前面的页数的数据。
--所以里层数据用来查询第2页前面的数据。
--外层的where 条件用来剔除第2页之前的数据。
2、offset … rows fetch next … rows only方式
3、ROW_NUMBER() OVER(ORDER BY 排序字段)
offset : 间隔多少条数据
五、同义词
--创建同义词
CREATE SYNONYM TEST.StudentSynonym FOR TEST.Student; --创建test数据库中student表的同义词