像下面的分页存储过程,如何将@wherestr
放进去查询语句中?
set ANSI_NULLS ON 
set QUOTED_IDENTIFIER ON 
go 
ALTER procedure [dbo].[UP_SH_User_List] 
(@status Int, 
@groupid Int, 
@usertype Int, 
@username nvarchar(20), 
@companyname nvarchar(100), 
@orderby Int, 
@pagesize int, 
@pageindex int, 
@docount bit) 
as 
declare @wherestr nvarchar(1000)  
declare @sql nvarchar(2000) 
if(@status>0) 
set @wherestr= ' and Status=' + convert(nvarchar(15),@status); if(@groupid>0) 
set @wherestr= @wherestr + ' and GroupID=' + convert(nvarchar(15),@groupid); if(@usertype>0) 
set @wherestr= @wherestr + ' and UserType=' + convert(nvarchar(15),@usertype); if(@username!='') 
set @wherestr= @wherestr + ' and UserName like %' + convert(nvarchar(100),@username) +'%'; if(@companyname!='') 
set @wherestr= @wherestr + ' and CompanyName like %' + convert(nvarchar(100),@companyname) +'%'; set @wherestr=' UserType=' + convert(nvarchar(15),@usertype); 
if(@docount=1) 
select count(*) from SH_User; else 
begin 
with temptbl as (SELECT ROW_NUMBER() OVER (ORDER BY UserID desc)AS Row, * from SH_User) 
SELECT * FROM temptbl where Row between (@pageindex-1)*@pagesize+1 and (@pageindex-1)*@pagesize+@pagesize 
end 

解决方案 »

  1.   


    set ANSI_NULLS ON 
    set QUOTED_IDENTIFIER ON 
    go 
    ALTER procedure [dbo].[UP_SH_User_List] 
    (@status Int, 
    @groupid Int, 
    @usertype Int, 
    @username nvarchar(20), 
    @companyname nvarchar(100), 
    @orderby Int, 
    @pagesize int, 
    @pageindex int, 
    @docount bit) 
    as 
    declare @wherestr nvarchar(1000)  
    declare @sql nvarchar(2000) 
    if(@status>0) 
    set @wherestr= ' Status=' + convert(nvarchar(15),@status); if(@groupid>0) 
    set @wherestr= @wherestr + ' and GroupID=' + convert(nvarchar(15),@groupid); if(@usertype>0) 
    set @wherestr= @wherestr + ' and UserType=' + convert(nvarchar(15),@usertype); if(@username!='') 
    set @wherestr= @wherestr + ' and UserName like %' + convert(nvarchar(100),@username) +'%'; if(@companyname!='') 
    set @wherestr= @wherestr + ' and CompanyName like %' + convert(nvarchar(100),@companyname) +'%'; set @wherestr=' UserType=' + convert(nvarchar(15),@usertype); 
    if(@docount=1) 
    exec('select count(*) from SH_User where ' +@wherestr); else 
    begin exec ('with temptbl as (SELECT ROW_NUMBER() OVER (ORDER BY UserID desc)AS Row, * from SH_User) 
    SELECT * FROM temptbl where Row between  ('+@pageindex+'-1)*'+@pagesize+'+1 and ('+@pageindex+'-1)*'+@pagesize+@pagesize +' and '+@wherestr)
    end 
      

  2.   

    大哥,exec ('with temptbl as (SELECT ROW_NUMBER() OVER (ORDER BY UserID desc)AS Row, * from SH_User) 
    SELECT * FROM temptbl where Row between  ('+@pageindex+'-1)*'+@pagesize+'+1 and ('+@pageindex+'-1)*'+@pagesize+@pagesize +' and '+@wherestr)
     这个是否错了啊?怎么显示不了数据?