日期:2014-05-18  浏览次数:20828 次

求救!!!紧急!!!存储过程分页问题
在用
CREATE PROCEDURE dbo.GetPagingRecord 
  ( 
  @tablename varchar(100),--表名或视图表 
  @fieldlist varchar(4000)='*',--欲选择字段列表 
  @orderfield varchar(100),--排序字段 
  @keyfield varchar(100),--主键 
  @pageindex int,--页号,从0开始 
  @pagesize int=15,--页尺寸 
  @strwhere varchar(4000),--条件 
  @ordertype bit=1--排序,1,降序,0,升序 
  ) 
AS 
/**//* 
名称:GetPagingRecord 
作用:按任意字段进行排序分页 
*/ 
  SET NOCOUNT ON 
  declare @sqlstr varchar(6000) 
  declare @count varchar(5000)
  --处理SQL中危险字符,并且将条件处理成易嵌入的形式 
/*
  set @strwhere=replace(@strwhere,'''','''''') 
  set @strwhere=replace(@strwhere,'--','') 
  set @strwhere=replace(@strwhere,';','') 
*/
  set @sqlstr='declare @CurPageNum int;' 
  set @sqlstr=@sqlstr+'declare @nextpagenum int;' 
  set @sqlstr=@sqlstr+'set @curpagenum='+cast(@PageIndex as varchar)+'*'+cast(@Pagesize as varchar)+';' 
  set @sqlstr=@sqlstr+'set @nextpagenum='+cast(@PageIndex+1 as varchar)+'*'+cast(@Pagesize as varchar)+';' 
  set @sqlstr=@sqlstr+'declare @sqlstr varchar(6000);' 
  set @count = ' select count(*) as Total from [' + @tablename + '] where 1=1 and '+ @strwhere  

  if @ordertype=1 
  begin 
  set @sqlstr=@sqlstr+'set @sqlstr=''select '+@fieldlist+' from ( select top ''+cast(@nextpagenum as varchar)+'' * from  
 
'+@tablename+' where '+ @strwhere +' order by '+@orderfield+' desc ) as a where '+@keyfield+' not in (  
 
select top ''+cast(@curpagenum as varchar)+'' '+@keyfield+' from '+@tablename+' where 1=1 and '+@strwhere+'  
 
order by '+@orderfield+' desc) order by '+@orderfield+' desc'';' 
  end 
  else 
  begin 
  set @sqlstr=@sqlstr+'set @sqlstr=''select '+@fieldlist+' from ( select top ''+cast(@nextpagenum as varchar)+'' * from  
 
'+@tablename+' where '+@strwhere+' + order by '+@orderfield+' asc ) as a where '+@keyfield+' not in (  
 
select top ''+cast(@curpagenum as varchar)+'' '+@keyfield+' from '+@tablename+' where 1=1 and '+@strwhere+'  
 
order by '+@orderfield+' asc) order by '+@orderfield+' asc'';' 
  end 
set @sqlstr=@sqlstr+'execute( @sqlstr)' 
 -- print @sqlstr 

  execute(@sqlstr)
  execute(@count)
GO
这个存储过程时。Where语句中不能添加Like的查询条件,添加以后会报错找不到列。
如:like '%aaa%'会提示找不到aaa列。

监视条件语句@strwhere = ' 1 = 1 and PointName Like ''%asdfas%'''
各位大侠多多帮忙



------解决方案--------------------
把存储过程写到查询分析器里,再用事件探查器跟踪一下看看。
------解决方案--------------------
如:like '%aaa%'会提示找不到aaa列。
--------------------------------------
这样的错误一般都出在varcher的字段上,因为没有单引号的缘故,你仔细检查程序看看
------解决方案--------------------
帅哥,解决这种组合的SQL报错的好办法是输出组合的最终SQL
事上你的那个存储过程已经有print @sqlstr 了,去掉注释,然后在查询分析器里执行存储过程
然后看看到底是执行的什么语句...