Ramco Ramco

Ramco Ramco

  • 443
  • 3.4k
  • 526.8k

Pivot Data with Cursors

Apr 28 2023 1:13 PM

Hi

  I want to use Pivot Data output for further processing with Cursors

select @cols = @cols + QUOTENAME(StudentName) + ',' from (Select distinct StudentName from #finalTable 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],[CoreRPL],[Lexile],[NumberOfPages],[SessionPlanStatus],[ClosingStock], ' + @cols + '
	INTO #temp from (select [BookID] ,BookTitle,[ComboBookName],[CoreRPL],[Lexile],[NumberOfPages],[SessionPlanStatus],Status,[ClosingStock],StudentName from #finalTable
	where (('''+@BookID+'''=''0'' OR [BookId] IN('+ cast(@BookID as varchar(Max))+')))) x pivot
	( Max(Status) for StudentName in (' + @cols + ')) piv order by BookTitle';

Thanks


Answers (2)