前台代码没问题,结合aspnetpager写的,@nickname 是通过搜索传过来的参数,把@nickname参数去掉后可以正常显示。 用EXECUTE执行存储过程,可以显示结果,但是不正常,比如每页是3条记录有时候它只会显示一个,或者有时候根据@pageindex 的变化会不显示,琢磨了半天了,也不知道错那哪了?
各位帮帮忙啊!谢了!
ALTER procedure [dbo].[GetUsersBySearch]
              (@pagesize int,
             @pageindex int,
             @docount bit,
@nickname varchar(255)
)
             as
             set nocount on
             if(@docount=1)
             select count(id) from users where 
nickname like '%'+@nickname+'%' and body like '%'+@body+'%'
             else
             begin
             declare @indextable table(id int identity(1,1),nid int)
             declare @PageLowerBound int
             declare @PageUpperBound int
             set @PageLowerBound=(@pageindex-1)*@pagesize
             set @PageUpperBound=@PageLowerBound+@pagesize
             set rowcount @PageUpperBound
             insert into @indextable(nid) select id from users order by regtime desc
             select O.* from users O,@indextable t where O.id=t.nid
and O.nickname like '%'+@nickname+'%'    //去掉查询后无任何问题,不知道此句哪里有问题?           
and t.id>@PageLowerBound and t.id<=@PageUpperBound order by t.id
             end
             set nocount off

解决方案 »

  1.   

    我给你这样一个存储过程吧CREATE PROCEDURE PageList@tblName   varchar(255),       -- 表名@strGetFields varchar(1000) = '*',  -- 需要返回的列 @fldName varchar(255)='',      -- 排序的字段名@PageSize   int = 10,          -- 页尺寸@PageIndex  int = 1,           -- 页码@doCount  bit = 0,   -- 返回记录总数, 非 0 值则返回@OrderType bit = 0,  -- 设置排序类型, 非 0 值则降序@strWhere  varchar(1500) = ''  -- 查询条件 (注意: 不要加 where)ASdeclare @strSQL   varchar(5000)       -- 主语句declare @strTmp   varchar(110)        -- 临时变量declare @strOrder varchar(400)        -- 排序类型 if @doCount != 0  begin    if @strWhere !=''    set @strSQL = "select count(*) as Total from [" + @tblName + "] where "+@strWhere    else    set @strSQL = "select count(*) as Total from [" + @tblName + "]"end  --以上代码的意思是如果@doCount传递过来的不是0,就执行总数统计。以下的所有代码都是@doCount为0的情况else
    beginif @OrderType != 0begin    set @strTmp = "<(select min"set @strOrder = " order by [" + @fldName +"] desc"--如果@OrderType不是0,就执行降序,这句很重要!endelsebegin    set @strTmp = ">(select max"    set @strOrder = " order by [" + @fldName +"] asc"end if @PageIndex = 1begin    if @strWhere != ''       set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ "  from [" + @tblName + "] where " + @strWhere + " " + @strOrder     else     set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ "  from ["+ @tblName + "] "+ @strOrder--如果是第一页就执行以上代码,这样会加快执行速度endelsebegin--以下代码赋予了@strSQL以真正执行的SQL代码set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ "  from ["    + @tblName + "] where [" + @fldName + "]" + @strTmp + "(["+ @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["+ @fldName + "] from [" + @tblName + "]" + @strOrder + ") as tblTmp)"+ @strOrder if @strWhere != ''    set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ "  from ["        + @tblName + "] where [" + @fldName + "]" + @strTmp + "(["        + @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["        + @fldName + "] from [" + @tblName + "] where " + @strWhere + " "        + @strOrder + ") as tblTmp) and " + @strWhere + " " + @strOrderend end   exec (@strSQL)
    GO
      

  2.   

    建议你把
    select O.* from users O,@indextable t where O.id=t.nid
    and O.nickname like '%'+@nickname+'%' 
    改成
    select O.* from users O left join @indextable t on O.id=t.nid
    where O.nickname like '%'+@nickname+'%'