declare @s nvarchar(1000),@aa int
set @s='SELECT @aa=COUNT(*) FROM ' + @tvar 
exec sp_executesql @s,N'@aa int out',@aa out
select 结果=@aa

解决方案 »

  1.   

    如果你的表有primary key 或唯一索引,
    你可以使用一下查询'select @aa=numrows from sysindexes 
        where objectid = (select objectid from sysobjects where name = '+@tab_var+
       ') and type=P'
    取得记录数,效率要高一些
      

  2.   

    'select @aa=numrows from sysindexes 
        where objectid = (select objectid from sysobjects where name = '+@tab_var+
       ') and type=P'这里的变量@aa在执行的时候,会有提示,没有申明,另外即使申明了,在执行完成后,也自动撤消了,不会交给接下来的程序使用!