日期:2014-05-19  浏览次数:20494 次

帮忙写一个最简单的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   @int_recordcount=@int_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)*@int_pagesize+1
      select   @int_endid   =   @int_beginid+@int_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)+@str_SQLWhere + ' order by sID desc '
exec sq_executesql @sql
------解决方案--------------------
sp_executesql