试试
insert into select * from exec(动态sql)

解决方案 »

  1.   

    insert into #tb exec(动态sql)不行,必须先创建表。
      

  2.   

    select * into #temp from exec(动态sql)
      

  3.   

    前提 #t已经创建,且结构一致
    insert into #t select * from exec(动态sql)
      

  4.   

    测试:
    select top 1 * into ##temp from sysobjects
    truncate table ##temp
    insert ##temp exec ('select * from sysobjects')
    select * from ##temp
    select * from sysobjects
    drop table ##temp
      

  5.   

    先取出一部分数据,然后存储过程中的其它查询都要用到该表,这个本身就是一个很复杂的查询,
    所以想用临时表保存以提高速度。列都是动态生成的,要是用create table 的话,怎么用?
      

  6.   

    先取出一部分数据,然后存储过程中的其它查询都要用到该表,这个本身就是一个很复杂的查询,
    所以想用临时表保存以提高速度。列都是动态生成的,要是用create table 的话,怎么用?if object_id('tempdb..##temp') is not null drop table ##temp
    exec ('create table ##temp (你的动态描述)')
    insert ##temp exe ('你的动态结果集')
      

  7.   

    可以呀,不過你得創建有兩個##的全局表。
    如果你用一個#,你exec(@sql)後就沒有了。