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 (
if object_id('tempdb..#t') is not null
drop table #t
select * into #t from (...) t