Tuesday, March 24, 2009

Nth Highest salary and Fetching column values for a Id as a single row

declare @vSQL varchar(1000)
declare @n int
set @n=5
set @vSQL='select Top 1 EmpSalary From
(Select Distinct Top ' + convert(varchar, @n) + ' EmpSalary
from EmpsInfo order by EmpSalary desc) a
order by EmpSalary'
exec(@vSQL)



declare @retstr varchar(8000)
select @retstr = COALESCE(@retstr + ',','') + cast(memno as varchar) from sample where sno=1
print @retstr

No comments: