1

I have column name with dates which comes from a table and changes from current month to last 41 month. and i have to write the column name again and again to reach the desired output. Is there any way I can take the column name dynamically.

Select product_Aggr.[ID], product_Aggr.group_code, product_Aggr.[Product_desciption], product_Aggr.Period, 
           product_Aggr.[Actual_Volume], product_Aggr.[Actual_Value]  
    Into Sales
    From 
         ( select * from ##temp  ) product
           cross apply (
                         values ( product.ID, product.group_code, product.[Product_desciption],'10/1/2015',product.[Total Quantity 2015-10], product.[Total Amount 2015-10]  ),
                                ..
                                ..
                                ..
                                ( product.ID, product.group_code, product.[Product_desciption],'10/1/2019', product.[Total Quantity 2019-01], product.[Total Amount 2019-01]   )

                        ) product_Aggr(ID, group_code, [Product_desciption], Period, [Actual_Quantity], [Actual_Amount])
Daniele Santi
  • 2,479
  • 1
  • 25
  • 22
user506566
  • 11
  • 2

1 Answers1

0

You could use dynamic SQL: Create you SQL string with ("procedural") SQL, and then use sp_executesql to execute your query.