USE [Second_CRMs]
GO
/****** Object:  StoredProcedure [dbo].[pagination]    Script Date: 06/24/2010 09:16:21 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO/**--分页存储过程**/
ALTER procedure [dbo].[pagination]
@count int=0 output,
@tableName varchar(255), -- 表名
@tableName2 varchar(255)='', --子表
@strGetFields varchar(1000)='*', --需要返回的列
@fldName varchar(255)='', --排序的字段名
@PageSize int=10, --页大小
@PageIndex int=1, --页码
@doCount bit=0, --返回记录总数, 非 0 值则返回
@OrderType bit=0, --设置排序类型, 非 0 值则降序
@strWhere varchar(1500)='' --查询条件 (注意: 不要加 where)


as
begin -- 存储过程开始 declare @strSQL   varchar(5000)       -- 主语句
declare @strTmp   varchar(110)        -- 临时变量
declare @strOrder varchar(400)        -- 排序类型
if(@doCount!=0)
begin
if(@strWhere!='')
begin
set @strSQL='select count(*) as Total from '+@tableName+' where '+@strWhere+''
end
else
begin
set @strSQL='select count(*) as Total from '+@tableName+''
end
set @count=@@ROWCOUNT
end
--以上代码的意思是如果@doCount传递过来的不是0,就执行总数统计。以下的所有代码都是@doCount为0的情况
else
begin
if(@OrderType!=0)
begin
set @strTmp = '<(select min'
set @strOrder = ' order by ' + @fldName +' desc'
end--如果@OrderType不是0,就执行降序,这句很重要!
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by ' + @fldName +' asc'
end
if(@PageIndex=1)
begin
if @strWhere != '' 
begin
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ '  from ' + @tableName + ' where ' + @strWhere + ' ' + @strOrder
end
else
begin
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ '  from '+ @tableName + ' '+ @strOrder
end
end--如果是第一页就执行以上代码,这样会加快执行速度
else
if(@strWhere!='')
begin
set @strSQL='select top '+str(@PageSize)+' '+@strGetFields+' from '+@tableName+' where '+@fldName+' '
+@strTmp+'('+@fldName+') from (select top '+str((@PageIndex-1)*@PageSize)+' '+@fldName+' from '+@tableName2+' where '+@strWhere+' '
+@strOrder+')as tblTmp)and '+@strWhere+' '+@strOrder+''
end
else
begin
set @strSQL='select top '+str(@PageSize)+' '+@strGetFields+' from '+@tableName+' where '+@fldName+' '
+@strTmp+'('+@fldName+') from (select top '+str((@PageIndex-1)*@PageSize)+' '+@fldName+' from '+@tableName2+' '
+@strOrder+')as tblTmp)'+@strOrder+''
end
endexec (@strSQL)end  --存储过程结束exec pagination @tableName='Bills a left join (select billId,sum(productTotalPrice) as ProductTotalPrice from BillAndProduct group by billId)b on a.bId=b.billId left join (select gBillId,sum(gPaymentPrice) as PaymentTotalPrice from Gathering group by gBillId)c on a.bId=c.gBillId left join (select tId,tValue from Type)f on a.bTypeId=f.tId',@fldName='bId'调用这个存储过程时候报错各位大侠帮我看看
Msg 102, Level 15, State 1, Line 1
'lef' 附近有语法错误。
Msg 102, Level 15, State 1, Line 1
'lef' 附近有语法错误。
Msg 102, Level 15, State 1, Line 1
'lef' 附近有语法错误。
Msg 102, Level 15, State 1, Line 1
'lef' 附近有语法错误。
Msg 102, Level 15, State 1, Line 1
'lef' 附近有语法错误。
Msg 102, Level 15, State 1, Line 1
'lef' 附近有语法错误。
Msg 102, Level 15, State 1, Line 1
'lef' 附近有语法错误。
Msg 102, Level 15, State 1, Line 1
'lef' 附近有语法错误。
Msg 102, Level 15, State 1, Line 1
'lef' 附近有语法错误。
Msg 102, Level 15, State 1, Line 1
'lef' 附近有语法错误。
Msg 102, Level 15, State 1, Line 1
'lef' 附近有语法错误。
Msg 102, Level 15, State 1, Line 1
'lef' 附近有语法错误。
Msg 102, Level 15, State 1, Line 1
'lef' 附近有语法错误。
Msg 102, Level 15, State 1, Line 1
'lef' 附近有语法错误。
Msg 102, Level 15, State 1, Line 1
'lef' 附近有语法错误。
Msg 102, Level 15, State 1, Line 1
'lef' 附近有语法错误。
Msg 102, Level 15, State 1, Line 1
'lef' 附近有语法错误。
Msg 102, Level 15, State 1, Line 1
'lef' 附近有语法错误。
Msg 102, Level 15, State 1, Line 1
'lef' 附近有语法错误。
Msg 102, Level 15, State 1, Line 1
'lef' 附近有语法错误。
Msg 102, Level 15, State 1, Line 1
'lef' 附近有语法错误。
Msg 102, Level 15, State 1, Line 1
'lef' 附近有语法错误。
Msg 102, Level 15, State 1, Line 1
'lef' 附近有语法错误。
Msg 102, Level 15, State 1, Line 1
'lef' 附近有语法错误。
Msg 102, Level 15, State 1, Line 1
'lef' 附近有语法错误。
Msg 102, Level 15, State 1, Line 1
'lef' 附近有语法错误。
Msg 102, Level 15, State 1, Line 1
'lef' 附近有语法错误。
Msg 102, Level 15, State 1, Line 1
'lef' 附近有语法错误。
Msg 102, Level 15, State 1, Line 1
'lef' 附近有语法错误。
Msg 102, Level 15, State 1, Line 1
'lef' 附近有语法错误。
Msg 102, Level 15, State 1, Line 1
'lef' 附近有语法错误。
Msg 217, Level 16, State 1, Procedure pagination, Line 73
超出了存储过程、函数、触发器或视图的最大嵌套层数(最大层数为 32)。

解决方案 »

  1.   

    你在exec (@strSQL)请加一个Print @strSQL看看语句对不对
      

  2.   

    错别字囧~~~
    你在exec (@strSQL)加一个Print @strSQL看看语句对不对
      

  3.   


    select top         10 *  from Bills a left join (select billId,sum(productTotalPrice) as ProductTotalPrice from BillAndProduct group by billId)b on a.bId=b.billId left join (select gBillId,sum(gPaymentPrice) as PaymentTotalPrice from Gathering group by gBillId)c on a.bId=c.gBillId lef  order by bId asc语句不对啊好像是只能连接2张表
      

  4.   


    USE [temp]
    GO
    /****** 对象:  StoredProcedure [dbo].[Bizsmooth_GetPagedResult]    脚本日期: 06/24/2010 12:41:14 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE PROCEDURE [dbo].[Bizsmooth_GetPagedResult]
    (
    @columnExpression nvarchar(Max),
    @filterExpression nvarchar(Max),
    @extraExpression nvarchar(Max),
    @sortExpression nvarchar(Max),
    @startRowIndex int,
    @maximumRows int
    )
    AS -- 确保指定了 @sortExpression
    IF LEN(@sortExpression) = 0
             SET @sortExpression = 'ID'SET @startRowIndex = @startRowIndex + 1-- 组合查询
    DECLARE @sql nvarchar(4000)
    SET @sql = 'SELECT ' + @columnExpression + 
               '  FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY ' + @sortExpression + ') AS RowRank ' +
               '          FROM ' + @filterExpression + ') AS Temp ' + @extraExpression;
    IF @maximumRows > 0            
            SET @sql = @sql + ' WHERE RowRank BETWEEN ' + STR(@startRowIndex) + ' AND ' + STR(@startRowIndex + @maximumRows);-- 执行SQL查询
    EXEC sp_executesql @sql
    我用的存储过程,你把几张表做成一个视图传入即可