#table  这个在哪儿

解决方案 »

  1.   

    #table这个不要管,肯定是存在的,我觉得是构造语句的问题!
      

  2.   

    出什么错误?是排序不正确还是错误。
    可不可以用固定变量替换一下,比如:(case when id in (1,2,3) then 1 else 0 end) desc 
      

  3.   


    --2005通用分页存储过程
    create procedure [dbo].[Pagination](
    @pagesize int, -- 页大小
    @pageindex int, -- 当前页
    @identity varchar(100), -- 唯一列
    @cells varchar(1000)='*', -- 显示列
    @tables varchar(1000), -- 表名称
    @condition varchar(2000)=NULL, -- 查询条件
    @top int = -1, -- 
    @orderby varchar(200)=NULL -- 排序 desc
    )
    as
    set nocount on
    declare @rowcount int;
    declare @SQL nvarchar(4000); set @SQL = N'select @rowcount = count(0) from ' + @tables + ' where 1=1 ';
        if(@condition IS NOT NULL) set @SQL = @SQL + @condition;
    exec sp_executesql @SQL,N'@rowcount int output',@rowcount output; if(@top != -1 and @rowcount > @top) set @rowcount = @top;    set @SQL = N'declare @___lb int;';
        set @SQL = @SQL + N'declare @___ub int;';
        set @SQL = @SQL + N'set @___lb=(@pageindex-1)*@pagesize;';
        set @SQL = @SQL + N'set @___ub=@___lb+@pagesize;';
        set @SQL = @SQL + N'set ROWCOUNT @___ub;'; set @SQL = @SQL + N'with ___t as (select *'; if(@orderby is not null and @orderby != '')
    set @SQL = @SQL + N',row_number() over(order by ' + @orderby + ',' + @identity + ' asc) as ___p from (';
    else
    set @SQL = @SQL + N',row_number() over(order by ' + @identity + ' asc) as ___p from ('; if(@top != -1)
    set @SQL = @SQL + N'select top ' + cast(@top as nvarchar(10)) + ' ';
    else
    set @SQL = @SQL + N'select '; if(@cells IS NOT NULL)
    set @SQL = @SQL + @cells;
    else
    set @SQL = @SQL + N'*'; set @SQL = @SQL + ' from ' + @tables + ' where 1=1'; if(@condition IS NOT NULL and @condition != '') set @SQL = @SQL + ' ' + @condition; set @SQL = @SQL + ') as ___q) select * from ___t where ___p>@___lb and ___p<=@___ub;'; exec sp_executesql @SQL,N'@pagesize int,@pageindex int',@pagesize = @pagesize,@pageindex = @pageindex; return @rowcount;
    set nocount off
      

  4.   

    太长,看偶得Create procedure [dbo].[uspCustomPaging] 
        @TableName varchar(50),                  --表或视图名
        @Fields varchar(5000) = '*',              --字段名(全部字段为*)
        @OrderFields varchar(5000),            --排序字段(必须!支持多字段,建议建索引)
        @SqlWhere varchar(5000) = '',         --条件语句(如and Name='a')
        @PageSize int,                                     --每页多少条记录
        @PageIndex int = 1 ,                           --指定当前为第几页
        @TotalPages int output                    --返回总页数 
    as
    begin
        declare @sql nvarchar(4000)
        declare @TotalRecords int       --计算总记录数及总页数     
        set @sql = 'select @TotalRecords = count(*) from ' + @TableName + ' where 1=1 ' + @sqlWhere
        exec sp_executesql @sql,N'@totalRecords int output',@TotalRecords output
        select @TotalPages=CEILING((@TotalRecords+0.0)/@PageSize)    --处理页数超出范围情况
        if @PageIndex<=0 
            set @PageIndex = 1
        if @PageIndex>@TotalPages
            set @PageIndex = @TotalPages    set @sql = 'select '+ @Fields + ' from (select top(@PageIndex*@PageSize) ' + @Fields + ',row_number() over(order by ' + @OrderFields + ') as rowNumber from ' + @TableName + ' where 1=1 ' + @SqlWhere + ') t where t.rowNumber >= ((@PageIndex-1)*@PageSize+1)'
        
        --print @Sql   
        exec sp_executesql @sql,N'@PageIndex int, @PageSize int',@PageIndex,@PageSize  
    end
      

  5.   

    :(case when id in (select id from #table) then 1 else 0 end) desc --改成 id desc 
    就不会有问题了吧order by中有计算引起的
      

  6.   


    这个很怪的,如果排序字段是这个查询表里面的字段,就不会出错,如果是自己构造的字段 比如 ... as title from table,然后用这个title排序就会出错