我需要在存储过程中创建一个列名只知道一部分,还有一部分不确定的临时表保存数据
下面是这个临时表需要保存数据的sql查询语句,其中只有ScanTime是确定的,后面的[CSI-10-01-N],[VAD-05-02-B]是作为参数传过来的,请问怎么创建这个临时表select ROW_NUMBER() OVER (order by ScanTime) RowNumber,ScanTime,[CSI-10-01-N],[VAD-05-02-B]
from
(
select COUNT(*) total,convert(varchar(100),ScanTime, 23) ScanTime,LocationCode 
from WLRS_Tallydata  where LocationCode in ('CSI-10-01-N','VAD-05-02-B') 
group by LocationCode,convert(varchar(100),ScanTime, 23)
) p
pivot
(
    max(total) for LocationCode in ([CSI-10-01-N],[VAD-05-02-B])
)
as X
原来这样创建if object_id('tempdb..#t') is not null
    drop table #t
    create table #t
    (
       id int,
       。。
    )
insert into #t select * from (貌似不可以了是吧

解决方案 »

  1.   

    用select * into 来直接生成,不行吗?
    if object_id('tempdb..#t') is not null
        drop table #tselect * into #t from (...) t
      

  2.   

    select 1 a into #A
    if OBJECT_ID('tempdb..#A') is not null
    drop table #Acreate table #A(A int )
    insert into #A
    select 1
    select * from #A
    SQL 2012 没有问题
      

  3.   

    方法1:
    select * into #temp from table1
    方法2:
    创建一个临时表包含ScanTime,[CSI-10-01-N],[VAD-05-02-B]三列,只是写数据进去的时候没有数据的就填空,也就是说创建一个大而全的临时表