create table #temp(.....)insert #temp exec(@sqlstr)

解决方案 »

  1.   

    set @sqlstr='select * from '+@tablename+' where eventid='+cast(eventid as varchar)
    insert into #aa exec(@sqlstr)
    select * from #aa
      

  2.   

    补充:
    #temp 的结构和返回的结果集相一致!
      

  3.   

    我想知道,如果我定义的是 表变量
    declare @t1 (...)
    是否可行?
    因为这个存储过程是经常被调用的,如果使用创建临时表,可能开销会很大。
      

  4.   

    --调用例子:--原存储过程,被调用的存储过程
    create proc p_1
    @tablename sysname,
    @eventid int
    as
    set @sqlstr='select * from '+@tablename+' where eventid='+cast(@eventid as varchar)
    exec(@sqlstr)
    go
    --调用的存储过程:
    create proc p_2
    as
    create table #t(与上面存储过程返回的表集的结构相同)
    insert into #t exec p_1
    go
      

  5.   

    我想知道,如果我定义的是 表变量
    declare @t1 (...)
    是否可行?
    因为这个存储过程是经常被调用的,如果使用创建临时表,可能开销会很大。答:
    存储过程的返回值不能存储到表变量中.
      

  6.   

    eg:
    create proc test 
    as
    select getdate() as gd然后
    declare @a table(abc datetime)
    insert @a exec test服务器: 消息 197,级别 15,状态 1,行 2
    EXECUTE cannot be used as a source when inserting into a table variable.
      

  7.   

    create table #temp(注意结构必须和返回结构一样 int)insert #temp exec(@sqlstr)
    不能用表变量