Ramco Ramco

Ramco Ramco

  • 419
  • 3.4k
  • 528.2k

Insert pivot data into temp table

Apr 27 2023 12:33 AM

Hi

  I have below Pivot & want to insert data into temp table

declare @cols as nvarchar(max)='';
declare @query as nvarchar(max)='';
select @cols = @cols + QUOTENAME(StudentName) + ',' from (Select distinct StudentName from View_SessionBookPlanningRecommendation where StudentID in (select [value] from string_split(@StudentID,','))) as tmp
select @cols = substring(@cols,0,len(@cols))
set @query = 'select [BookId] [Book ID],BookTitle [Book],[ComboBookName],[Core RPL],[Lexile],[NumberOfPages],[Session Plan Status],[ClosingStock], ' + @cols + ' from (select [BookID] ,BookTitle,[ComboBookName],[Core RPL],[Lexile],[NumberOfPages],[Session Plan Status],Status,[ClosingStock],StudentName from View_SessionBookPlanningRecommendation where (('''+@BookID+'''=''0'' OR [BookId] IN('+ cast(@BookID as varchar(Max))+')))) x pivot
( Max(Status) for StudentName in (' + @cols + ')) piv order by BookTitle';
print @query
execute (@query) 

Thanks


Answers (5)