create procedure [GetShopsByTypeCity]                                                                          
  (
   @Type tinyint,
   @CityId tinyint,
   @pageindex int,                                                                              
 @pagesize int                                                                     
)                                                                        
as
           
              select top @pagesize O.Id,O.[Name],O.Phone,O.Address,O.CommentNumber,O.ClickTimes,O.RecentId,O.RecentHead,O.RecentContent,
                   case 
              when Rate>=1 and Rate<1.25 then '1'
              when Rate>=1.25 and Rate<1.75 then '1.5'
              when Rate>=1.75 and Rate<2.25 then '2'
              when Rate>=2.25 and Rate<2.75 then '2.5'
              when Rate>=2.75 and Rate<3.25 then '3'
              when Rate>=3.25 and Rate<3.75 then '3.5'
              when Rate>=3.75 and Rate<4.25 then '4'
              when Rate>=4.25 and Rate<4.75 then '4.5'
              when Rate>=4.75 and Rate<=5 then '5'
              end as Rate                 from ShopsList O where id not in (select top @pagesize*(@pageindex-1) id from shopslist)
         and SubType=@Type AND City=@CityId   order by  descselect top @pagesize  
还有 select top @pagesize*(@pageindex-1) id from shopslist 这里错误,难道不能用参数表示吗?

解决方案 »

  1.   

    select top @pagesize*(@pageindex-1) id from shopslist
    这个只能用动态的
      

  2.   

    SQL Server 2000是不支持select top @pagesize 这种写法的,需要用exec(@sql)这种形式
    例如:
    CREATE PROCEDURE dbo.fun&nbsp;
    &nbsp; &nbsp; @top INT&nbsp;
    AS&nbsp;
    BEGIN&nbsp;
    &nbsp; &nbsp; DECLARE @sql VARCHAR(255)&nbsp;
    &nbsp; &nbsp; SET @sql = 'SELECT TOP ' + CONVERT(VARCHAR, @top) + ' foo'&nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; + ' FROM blat'&nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; + ' ORDER BY foo DESC'&nbsp;
    &nbsp; &nbsp; EXEC(@sql)&nbsp;
    END&nbsp;
    GOSQL Server 2005支持你那种写法
      

  3.   

    CREATE PROCEDURE dbo.fun(@top INT)
    AS 
    BEGIN
    &nbsp; &nbsp; DECLARE @sql VARCHAR(255)&nbsp;
    &nbsp; &nbsp; SET @sql = 'SELECT TOP ' + CONVERT(VARCHAR, @top) + ' foo'&nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; + ' FROM blat'&nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; + ' ORDER BY foo DESC'&nbsp;
    &nbsp; &nbsp; EXEC(@sql)
    END
      

  4.   

    把select top @pagesize*(@pageindex-1) id from shopslist语句改写为declare @sqlstr char(400)
    declare @resize int 
    set @resize= @pagesize*(@pageindex-1)
    set @sqlstr='select top '+@resize+'id from shopslist语句'
    exec @sqlstr