当前位置: 代码迷 >> Sql Server >> 帮忙写一个最简单的SQL存储过程,以下是一个分页的在存储过程。解决方案
  详细解决方案

帮忙写一个最简单的SQL存储过程,以下是一个分页的在存储过程。解决方案

热度:4   发布时间:2016-04-27 20:29:18.0
帮忙写一个最简单的SQL存储过程,以下是一个分页的在存储过程。
CREATE   PROCEDURE   dbo.PPOK_HousePage
(

@int_pagenow   int=0,
@int_pagesize   int=0,
@str_SQLWhere   varchar(222),
@int_recordcount   int=0   output

)

AS
set   nocount   on

declare   @int_allid   int
declare   @int_beginid   int,@int_endid   int
declare   @int_pagebegin   int,   @int_pageend   int

      select   @int_allid=count(sID)   from   PPOK_House
      select   @[email protected]_allid

      declare   cro_fastread   cursor   scroll
      for
          select   sID   from   PPOK_House   order   by   sID   desc

      open   cro_fastread
      select   @int_beginid=(@int_pagenow-1)[email protected]_pagesize+1
      select   @int_endid   =   @[email protected]_pagesize-1

      fetch   absolute     @int_beginid   from   cro_fastread   into   @int_pagebegin
      if   @int_endid> @int_allid
              fetch   last   from   cro_fastread   into   @int_pageend
      else
              fetch   absolute   @int_endid   from   cro_fastread   into   @int_pageend

      select   *   from   PPOK_House   where   sID   between   @int_pageend   and   @int_pagebegin
order   by   sID   desc

      close   cro_fastread
      deallocate   cro_fastread

      return
GO

====================================================

我想在最后一句添上   @str_SQLWhere   变量该如何做呢?谢谢了。
即这句:

      select   *   from   PPOK_House   where   sID   between   @int_pageend   and   @int_pagebegin
order   by   sID   desc


------解决方案--------------------
declare @sql nvarchar(5000)
set @sql= 'select * from PPOK_House where sID between '+ltrim(@int_pageend)+ ' and '+ltrim(@int_pagebegin)[email protected]_SQLWhere + ' order by sID desc '
exec sq_executesql @sql
------解决方案--------------------
sp_executesql
  相关解决方案