Dynamic SQL Pivot by Column SQL Server?

It might look like this: declare @tableName nvarchar(128) = N'table_with_50_columns' declare @columnLikeFilter nvarchar(128) = N'someName%' declare @columns nvarchar(2000) = ''; declare @sumColumns nvarchar(2000) = ''; select @columns = @columns + COLUMN_NAME + ',', @sumColumns = @sumColumns + 'sum(' + COLUMN_NAME + ') as ' + COLUMN_NAME + ',' from INFORMATION_SCHEMA. COLUMNS where TABLE_NAME = @tableName and COLUMN_NAME like @columnLikeFilter order by ORDINAL_POSITION ; set @columns = left(@columns, len(@columns) - 1) ; set @sumColumns = left(@sumColumns, len(@sumColumns) - 1) ; declare @sql nvarchar(4000) = N';with cteColumnts (ORDINAL_POSITION, COLUMN_NAME) as ( select ORDINAL_POSITION, COLUMN_NAME from INFORMATION_SCHEMA. COLUMNS where TABLE_NAME = N'''+ @tableName + ''' and COLUMN_NAME like ''' + @columnLikeFilter + ''' ), cteValues (ColumnName, SumValue) as ( SELECT ColumnName, SumValue FROM (SELECT ' + @sumColumns + ' FROM dbo.' + @tableName + ') p UNPIVOT (SumValue FOR ColumnName IN (' + @columns + ') )AS unpvt ) select row_number() over(order by ORDINAL_POSITION) as ID, ColumnName, SumValue from cteColumnts c inner join cteValues v on COLUMN_NAME = ColumnName order by ORDINAL_POSITION' --print @sql exec sp_executesql @sql.

I think this blog post gives an answer to your question: simple-talk.com/community/blogs/andras/a....

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions