三张表:
用户表,角色表,用户角色关系表
三表连接分页sql语句,用户和角色是一对多的关系,一个用户可以有多个角色。
我的代码如下:            
select count(*) from dbo.Users a,dbo.Role b,dbo.UserRole c where a.Uid=c.Uid and b.RoleId=c.RoleId and del=1;select top " + pagesize + " * from (select a.*,b.RoleName from dbo.Users a,dbo.Role b,dbo.UserRole c where a.Uid=c.Uid and b.RoleId=c.RoleId and del=1)a where Uid not in(select top " + ((pageindex - 1) * pagesize) + " Uid from(select a.*,b.RoleName from dbo.Users a,dbo.Role b,dbo.UserRole c where a.Uid=c.Uid and b.RoleId=c.RoleId and del=1)a order by Uid desc) order by Uid desc";按角色显示,当显示所有角色时就有重复的用户,这些重复用户又是以不同的角色存在的。
请教各位高手!

解决方案 »

  1.   

    set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    go
    --DECLARE @sqlstr nvarchar(4000)
    ----查询字符串
    --DECLARE @currentpage int
    ----第N页
    --DECLARE @pagesize INT
    --SET @sqlstr='SELECT * FROM SJ_Radio'
    --SET @currentpage=1
    --SET @pagesize=20
    --
    --EXEC sp_Info_GetRecordByPage @sqlstr,@currentpage,@pagesize
    ALTER procedure [dbo].[sp_Info_GetRecordByPage]
    (
    @sqlstr nvarchar(4000),
    --查询字符串
    @currentpage int,
    --第N页
    @pagesize int
    --每页行数
    )
    as
    set nocount ondeclare @P1 int,
    --P1是游标的id
    @rowcount intexec sp_cursoropen @P1 output,@sqlstr,@scrollopt=1,@ccopt=1,@rowcount=@rowcount output--,@rowcount as 总行数,@currentpage as 当前页set @currentpage=(@currentpage-1)*@pagesize+1exec sp_cursorfetch @P1,16,@currentpage,@pagesize--select ceiling(1.0*@rowcount/@pagesize+1) as totalPageNum,@rowcount as totalRecord select @rowcount as totalRecord exec sp_cursorclose @P1set nocount off这是我目前所用过最快的分页存储过程,需要传入的参数自己看吧