My table is this,
Staff_Name Question SelfEvaluation ManagementEvaluation
Nirmal Job Efficiency 8 5
Narasiman Job Efficiency 7 6
sudha Job Efficiency 7 5
I want output like this,
Question Nirmal_SelfEval MngmtEval sudha_SelfEval MngmtEval Narasiman_SelfEval MngmtEval
Job Efficiency 8 5 7 5 7 6
Please help me
Loading

saravanagopi sPosted Apr 9, 2015, 5:51 AM
Declare @outputstr varchar(100)
Declare @Table table(Row int,question varchar(100),Self_eval varchar(30),Mgmt_eval varchar(30))
DECLARE efficiency CURSOR FOR select distinct job_efficiency from tablename
OPEN efficiency;
FETCH NEXT FROM efficiency into @Job;
WHILE @@FETCH_STATUS = 0
BEGIN
insert into @Table
select job_efficiency,self_eval,mgmt_eval from tablename where question = @job
--Inner cursor
Declare @job_efficiency varchar(30)
Declare @self varchar(10)
Declare @Mgmt varchar(10)
declare cursor_inner cursor for select job_efficiency,self_eval,mgmt_eval from @Table
open Cursor_inner
Fetch Next from cursor_inner into @job_efficiency,@self,@mgmt
while @@Fetch_status = 0
Begin
if(len(@outputstr) > 0)
Begin
set @outputstr = cast(@outputstr as varchar(100)) + ' ' + cast(@self as varchar(100)) + ' ' + cast(@mgmt as varchar(100))
End
else
begin
set @outputstr = cast(@job_efficiency as varchar(100)) + ' ' + cast(@self as varchar(100)) + ' ' + cast(@mgmt as varchar(100))
end
Fetch Next from cursor_inner into @Question,@self,@mgmt
End
close cursor_inner
deallocate cursor_inner
-- closed inner cursor
delete from @Table
FETCH NEXT FROM efficiency into @Job;
END;
select @outputstr
CLOSE efficiency;
DEALLOCATE efficiency;
Shubham KumarPosted Apr 9, 2015, 5:32 AM
https://technet.microsoft.com/en-us/library/ms177410%28v=sql.105%29.aspx
just go though this link