Getting a Dynamically-Generated Pivot-Table into a Temp Table

前端 未结 4 1321
挽巷
挽巷 2020-12-18 10:32

I\'ve seen this, so I know how to create a pivot table with a dynamically generated set of fields. My problem now is that I\'d like to get the results into a temporary table

4条回答
  •  隐瞒了意图╮
    2020-12-18 10:52

    Let me try this explanation of select into instead. I'm running SQL Server 2005 as well. Because you have PIVOT tables I'm going to assume the same or 2008.

    select 
        o.*,
        OtherField1,
        OtherField2
    INTO #temp
    FROM
        OriginalOtherData as ood
    PIVOT (
        MAX([Value])
        FOR Field in (OtherField1, OtherField2)
    ) as piv
    RIGHT OUTER join
        Original o on o.OriginalSD = piv.OriginalSD
    
    select * from #temp
    Drop table #temp
    

    The only difference between a normal select and a select into is that INTO #table part.

提交回复
热议问题