临时表@tb如何从临时表里面取出 前面 的  几百条语句这个 top 数据 是动态的top 100 或者
top 200 或者
top 300 或者
top 400
这种语句定义了一个临时表@tb了,并且里面语句有数据@str='select top '+str(@count)+'.........请问这句要怎么写??我这里怎么总是提示:必须声明标量变量 "@tb"exec(@str)

解决方案 »

  1.   

    一般表也不一定顺序固定,加个order by 吧。
      

  2.   

    不是顺序问题,我现在是要能从临时表里面查询出所要的条数就行了带参数的SQL语句  这个不知道怎么写
      

  3.   

    create table #tb
    (
     id int
    )
    declare @i int;
    set @i=0
    while @i<1000
    begin
    insert into #tb values(@i);
    set @i=@i+1;
    endselect top(100)* from #tb
    create proc aaa
     @count int 
    as
    begin
     select top(@count)* from #tb
    end
    exec aaa 100
    --试试这个是你要的结果不是
      

  4.   

    对@tb的定义也写在动态语句@str中
      

  5.   


    declare @sql as nvarchar(4000)
    declare @count as int
    set @count=100  --200,300,400
    set @sql=' declare @tb table(id int,[name] nvarchar(10))'+   --把这里改成你的临时表@tb 的定义
             ' insert @tb select * from table '+        --把这里改成你的insert 语句
             ' select top '+left(@count)+' from @T'
    exec(@sql)