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
 looping problem

Author  Topic 

de4ever@gmail.com
Starting Member

36 Posts

Posted - 2010-03-26 : 03:46:49
i have a table
table1
pkVal name id1 id2 id3 id4 id5
1 test 2 3 5 8 2

i want the output
for @id= 2
as
-----------
1 test 2
1 test 2

waiting for your replies thanks

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-03-26 : 04:05:11
what is the version of SQL Server are you using ?


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

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-26 : 11:22:33
if SQL 2005 & later


select pkVal, name,val
from table1
unpivot (val for id in ([id1],[id2],[id3],[id4],[id5]))u
where val=2


if earlier versions

select pkval,name,id
from
(
select pkval,name,id1 as id
from table1
union all
select pkval,name,id2
from table1
union all
select pkval,name,id3
from table1
union all
select pkval,name,id4
from table1
union all
select pkval,name,id5
from table1
)t
where id = 2


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -