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