declare @sql nvarchar(4000)
set @sql = N'select * into #tempa from 表A'
exec (@sql)我用select * from #tempa查询出临时表中的数据,它报#tempa不存在,在字符串中定义的临时表不能在外面用吗?

解决方案 »

  1.   

    create table #tempa()  --和表A的字段一样
      

  2.   

    表数据有两种方式:create table 表
    go
    insert into 表select * into 表 from 表A
      

  3.   

    好像答过了,用全局的 ##tempa
      

  4.   

    --declare @sql nvarchar(4000)
    select * into #tempa from 表A
    --这样就既创建临时表,也导入了数据。没有什么变量与动态的SQL,不用exec也可以的
    --exec (@sql)