SQL server高效分页

     从mysql转成SQL server,各种不习惯。先说说分页

    SQL Server 常用的分页方式大概有四种,一看,各种子查询,表示头大。

    这里选择了一个效率较高的分页方式来说明:

分页前:

select uc.Workcode,hr.lastname,hd.departmentname,uc.JoinWorkDate,uc.modedatacreatedate,fm.secname as secname,ms.name as status  from ufc uc 
left join a hr on hr.id=uc.HrmId 
left join b hd on hd.id=uc.DepId 
left join c fm on fm.seclevel=uc.JobLevel
left join d ms on CAST(ms.disorder as int)=uc.Status and ms.mainid=12

 

分页:

SELECT TOP 页大小 *
FROM
    (
        SELECT ROW_NUMBER() OVER (ORDER BY id) AS RowNumber,* FROM table1
    )   as A 
WHERE RowNumber > 页大小*(页数-1)

 

对上述SQL进行分页:(第一页,每页10条)

SELECT TOP 10 *
FROM
    (
        SELECT ROW_NUMBER() OVER (ORDER BY uc.modedatacreatedate) AS     
RowNumber,uc.Workcode,hr.lastname,hd.departmentname,uc.JoinWorkDate,uc.modedatacreatedate,fm.secname as secname,ms.name as status  from ufc uc 
         left join a hr on hr.id=uc.HrmId 
         left join b hd on hd.id=uc.DepId 
         left join c fm on fm.seclevel=uc.JobLevel
         left join d ms on CAST(ms.disorder as int)=uc.Status and ms.mainid=12
     )   as A 
WHERE RowNumber > 10*(1-1)

 

你可能感兴趣的:(---------SQL,Server,☆数据库)