set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[Getnewsclass]
@pageSize int, --页大小
@pageIndex int, --页索引
@TableName varchar(50),   --表明
@Filter   varchar(1000) /*,
@rowsCount int output --总记录数*/
AS
BEGIN
--计算总行数
declare @rowsCount int
select @rowsCount = count(*) from @TableName declare @startRecord int, @maxRecord int
set @startRecord = (@pageIndex - 1) * @pageSize
    set @maxRecord = @pageIndex * @pageSize
    if (@startRecord >= @rowsCount and @startRecord >= @pageSize)
    begin
        set @startRecord = @startRecord - @pageSize
        set @maxRecord = @maxRecord - @pageSize
    end
declare @Statment nvarchar(4000)
set @Statment ='SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY  id) as rowNum,* from '+@TableName+' where  '+@Filter+')
 as t  where  
rowNum > '+cast(@startRecord as nvarchar)+' and 
rowNum <= '+cast(@maxRecord as nvarchar)
exec sp_executesql @StatmentEND
消息 1087,级别 15,状态 2,过程 Getnewsclass,第 17 行
必须声明表变量 "@TableName"。
这东西不是很熟~!请指教~!

解决方案 »

  1.   

    你调用这个存储过程是怎么调用的呢,显然是少个了参数@TableName
      

  2.   

    from '+@TableName+' where '+@Filter+')
    这个应该不用加引号和加号吧
      

  3.   

    我在sql里执行就报这错了,和调用还没关系呢~!
      

  4.   

    原因是你没有把变量声明…本来是有的但是 最致命的就是你写存储过程带进来的变量没有放进括号里
    应该如下:
    ALTER PROCEDURE [dbo].[Getnewsclass]
    (
    @pageSize int, --页大小
    @pageIndex int, --页索引
    @TableName varchar(50), --表明
    @Filter varchar(1000) /*,
    @rowsCount int output --总记录数*/
    )
    AS
    BEGIN
      

  5.   

    4楼的不行啊   我放了,可在执行的时候就报"@TableName"没有声明啊!
      

  6.   

    select @rowsCount = count(*) from @TableName
      

  7.   

    --select @rowsCount = count(*) from @TableName
    sp_executesql N'select @rowsCount = count(*) from '+@TableName,N'@rowsCount int output',@rowsCount output
      

  8.   

    set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    go-- =============================================
    -- Author: <Author,,Name>
    -- Create date: <Create Date,,>
    -- Description: <Description,,>
    -- =============================================
    ALTER PROCEDURE [dbo].[Getnewsclass]
    @pageSize int, --页大小
    @pageIndex int, --页索引
    @TableName varchar(50), --表明
    @Filter varchar(1000) /*,
    @rowsCount int output --总记录数*/
    AS
    BEGIN
    --计算总行数
    declare @rowsCount int
    --select @rowsCount = count(*) from @TableName
    sp_executesql N'select @rowsCount = count(*) from '+@TableName,N'@rowsCount int output',@rowsCount output
    declare @startRecord int, @maxRecord int
    set @startRecord = (@pageIndex - 1) * @pageSize
    set @maxRecord = @pageIndex * @pageSize
    if (@startRecord >= @rowsCount and @startRecord >= @pageSize)
    begin
    set @startRecord = @startRecord - @pageSize
    set @maxRecord = @maxRecord - @pageSize
    end
    declare @Statment nvarchar(4000)
    set @Statment ='SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY id) as rowNum,* from '+@TableName+' where '+@Filter+')
    as t where 
    rowNum > '+cast(@startRecord as nvarchar)+' and 
    rowNum <= '+cast(@maxRecord as nvarchar)
    exec sp_executesql @StatmentEND
      

  9.   

    把 @TableName 放入字符串去应该就OK 了
      

  10.   

    9楼的,有这个错误,你的~!消息 102,级别 15,状态 1,过程 Getnewsclass,第 18 行
    '+' 附近有语法错误。
      

  11.   

    不能这样写吧 应该是组SQL 用sp_executesql执行组好的SQL 
      

  12.   

    执行的时候报这个错@TableName是表名啊
    是传进来的,但在sql里执行的时候报这个错~!
      

  13.   

    变量只能写在where 后面的语句中. 你这种只能用9楼的写法.
      

  14.   

    set @strTmp = 'select @Counts=Count(' + @ID + ') FROM '+@tblName
    exec sp_executesql @strTmp,N'@Counts int out ',@Counts out
      

  15.   

    @Filter varchar(1000) /*,这句有问题,你缺少逗号
      

  16.   

    'SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY id) as rowNum,* from '+@TableName+' 
      

  17.   

    昨天的问题啊,还是不行啊,大侠救命啊~!set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    go
    -- =============================================
    -- Author: <Author,,Name>
    -- Create date: <Create Date,,>
    -- Description: <Description,,>
    -- =============================================
    ALTER PROCEDURE [dbo].[Getnewsclass]
    @pageSize int, --页大小
    @pageIndex int, --页索引
    @TableName nvarchar(1000),   --表明
    @Filter   nvarchar(1000) /*,
    @rowsCount int output --总记录数*/
    AS
    BEGIN
    --计算总行数
    declare @rowsCount int
    declare @strTmp nvarchar(1000)
    set  @strTmp=N'select @rowsCount=Count(id) FROM '+@TableName
    exec sp_executesql @strTmp
    declare @startRecord int, @maxRecord int
    set @startRecord = (@pageIndex - 1) * @pageSize
        set @maxRecord = @pageIndex * @pageSize
        if (@startRecord >= @rowsCount and @startRecord >= @pageSize)
        begin
            set @startRecord = @startRecord - @pageSize
            set @maxRecord = @maxRecord - @pageSize
        end
    declare @Statment nvarchar(4000)
    set @Statment ='SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY  id) as rowNum,* from '+@TableName+' where  '+@Filter+')
     as t  where  
    rowNum > '+cast(@startRecord as nvarchar)+' and 
    rowNum <= '+cast(@maxRecord as nvarchar)
    exec sp_executesql @StatmentEND
    必须声明标量变量 "@rowsCount"。 
      

  18.   

    @rowsCount 上面一行少了个逗号?
      

  19.   

    exec sp_executesql @strTmp--替换为:exec sp_executesql @strTmp,N'@rowsCountint out ',@rowsCountout
      

  20.   

     /*,
        @rowsCount        int output        --总记录数*/
    /*,被你注释掉了,哪还有@rowsCount
      

  21.   

    谢谢大家了,在大家的帮助下,改来改去终于OK了~!下面是OK的版本,分不多,谢谢大家了set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    go
    -- =============================================
    -- Author: <Author,,Name>
    -- Create date: <Create Date,,>
    -- Description: <Description,,>
    -- =============================================
    ALTER PROCEDURE [dbo].[Getnewsclass]
    @pageSize int, --页大小
    @pageIndex int, --页索引
    @TableName nvarchar(1000),   --表明
    @Filter   nvarchar(1000)

    AS
    BEGIN
    --计算总行数
    declare @rowsCount int 
    declare @strTmp nvarchar(1000)
    set  @strTmp='select @rowsCount=Count(id) FROM '+@TableName
    exec sp_executesql @strTmp,N'@rowsCount int',@rowsCount
    declare @startRecord int, @maxRecord int
    set @startRecord = (@pageIndex - 1) * @pageSize
        set @maxRecord = @pageIndex * @pageSize
        if (@startRecord >= @rowsCount and @startRecord >= @pageSize)
        begin
            set @startRecord = @startRecord - @pageSize
            set @maxRecord = @maxRecord - @pageSize
        end
    declare @Statment nvarchar(4000)
    set @Statment ='SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY  id) as rowNum,* from '+@TableName+' where  '+@Filter+')
     as t where  
    rowNum > '+cast(@startRecord as nvarchar)+' and 
    rowNum <= '+cast(@maxRecord as nvarchar)
    exec sp_executesql @StatmentEND