日期:2014-05-17  浏览次数:20585 次

sql server存储过程中创建列名不确定的临时表
我需要在存储过程中创建一个列名只知道一部分,还有一部分不确定的临时表保存数据


下面是这个临时表需要保存数据的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 (


貌似不可以了是吧

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

select * into #t from (...) t

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