Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 UNPIVOT Help

Author  Topic 

learntsql

524 Posts

Posted - 2009-08-03 : 03:10:00
Hi All,

--Unpivot the table.
SELECT VendorID, Employee, Orders
FROM
(SELECT VendorID, Emp1, Emp2, Emp3, Emp4, Emp5
FROM pvt) p
UNPIVOT
(Orders FOR Employee IN
(Emp1, Emp2, Emp3, Emp4, Emp5)
)AS unpvt

In above Query how can i dynamically get the Emp1,Emp2,Emp3.... columns which are mentioned in UNPIVOT Clause and SELECT clause from table itself.
Because my table structure may change.
Is it possible? If not if that is the requirement what are the alternative ways.
Plz tell me.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-08-03 : 03:13:03
you have to use Dynamic SQL.

read here for more information
The Curse and Blessings of Dynamic SQL


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

learntsql

524 Posts

Posted - 2009-08-03 : 03:22:23
Thanks for reply,
but it slows down the performance
and we have security issues also right.
Go to Top of Page

learntsql

524 Posts

Posted - 2009-08-03 : 03:26:19
Oh Sorry khtan,
I will go through once that link.
Go to Top of Page
   

- Advertisement -