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
 unpivoting

Author  Topic 

mhdmehraj
Starting Member

6 Posts

Posted - 2010-04-08 : 06:02:57
Hi all,
i need to unpivot a multiple tables , so i need to incorporate the unpivote query into a stored procedure , with the input parameter as table name .

select * from test;

IDNO A B C
---------- ---------- ---------- ----------
1 1 2 3
2 4 5 6
3 7 8 9

my output should be like

COL_ROW COL_name VALUE
---------- ---- ----------
1 A 1
1 B 2
1 C 3
2 A 4
2 B 5
2 C 6
3 A 7
3 B 8
3 C 9

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-04-08 : 06:09:29
Start with


select IDNO,'A' as col_name, A as col_value from table
union all
select IDNO,'B' as col_name, B as col_value from table
union all
select IDNO,'C' as col_name, C as col_value from table


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

mhdmehraj
Starting Member

6 Posts

Posted - 2010-04-08 : 06:11:05
can u please elaborate it
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-04-08 : 06:13:11
quote:
Originally posted by mhdmehraj

can u please elaborate it


Run the individual queries and you may get an idea

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -