create proc pageRepeat
@tblname varchar(255),--table name
@fldname varchar(255),--key field name
@pageSize int,
@pgIndex int,
@rowCount int output,---return row count **********************************
@pageNum int output---return page number ********************************************
as
declare @strSQL varchar(8000),---main qurey
@totalCounts int,
@pageCounts int
set @pageCounts=(@totalCounts/@pageSize) --********************************** output variableset @strSQL='select top '+cast(@pageSize as varchar)+'* from '+@tblname+' where '+@fldname
+' >(select '+cast(@pageSize*(@pageCounts-@pgIndex)+@fldname+' from '+@tblname+' order by '
+@fldname+' desc) order by '+@fldname+' asc 'if @pgIndex = 1
begin
set @strSQL='select top'+cast(@pageSize as varchar)+'* from'+@tblname+' order by'+@fldname+' des'
end
 
declare @sql varchar(400)
set @sql=N'select @totalCounts=count(*) from @tblname' --********************************** output variable
exec sp_executesql @strSQL,@sql,@rowCount out,@pageNum out查询分析说.
服务器: 消息 156,级别 15,状态 1,过程 pageRepeat,行 22
在关键字 'if' 附近有语法错误

但是我怎样改都有错??谢谢大家帮忙.我初学.

解决方案 »

  1.   

    if (@pgIndex = 1)
    begin
    set @strSQL='select top'+cast(@pageSize as varchar)+'* from'+@tblname+' order by'+@fldname+' des'
    end
      

  2.   

    exec sp_executesql @strSQL,@sql,@rowCount out,@pageNum out
    ------------????
      

  3.   

    set @strSQL='select top '+cast(@pageSize as varchar)+'* from '+@tblname+' where '+@fldname
    +' >(select '+cast(@pageSize*(@pageCounts-@pgIndex)+@fldname+' from '+@tblname+' order by '
    +@fldname+' desc) order by '+@fldname+' asc '这句话的问题
    具体在哪里一时也没找出来
      

  4.   

    declare @strsql nvarchar(8000)
    if @pgIndex = 1    set @strSQL=N'select top'+cast(@pageSize as varchar)+'* from'+@tblname+' order by'+@fldname+' des'else
        set @strSQL=N'select @totalCounts=count(*) from @tblname' --******** output variable
    exec sp_executesql @strSQL,@rowCount out,@pageNum out
      

  5.   

    恩.那里应该错的.我只是想输出  ,@rowCount ,@pageNum  这2个变量.应该错了.
      

  6.   

    set @strSQL='select top '+cast(@pageSize as varchar)+'* from '+@tblname+' where '+@fldname
    +' >(select '+cast(@pageSize*(@pageCounts-@pgIndex)+@fldname+' from '+@tblname+' order by '
    +@fldname+' desc) order by '+@fldname+' asc '
    这个里面的cast没有下文
      

  7.   

    [size=24px]哦!!是.cast~~
    谢谢大家,.请问 怎样 SQL 的查询分析器 这么 不智能..
    查询分析器不告诉我准确的错误位置?只是告诉我其他的地方的错.最后根本不是那里错.[/size]
      

  8.   


    declare @strSQL varchar(8000),---main qurey
    需要定义为 nvarchar(8000)
    declare @sql varchar(400)
    改成:
    declare @sql nvarchar(400)因为后面用到了Exec sp_executesql 
      

  9.   

    谢谢大家,.请问 怎么SQL 的查询分析器 这么 不智能.. 
    查询分析器不告诉我准确的错误位置?只是告诉我其他的地方的错.最后根本不是那里错