Resolves #80952 and also fixes arbitrary rearrangements of pivoted columns (dropping, duplicating and reordering). The bug was caused because fields reordering in `ProjectExec` wrapping `PivotExec` ...
DECLARE @total_col VARCHAR(MAX) DECLARE @result NVARCHAR(MAX) SET @total_col='' SELECT @total_col = @total_col + QUOTENAME(item) + ',' FROM items SET @result=LEFT(@total_col, (LEN(@total_col)-1)) ...