First import the data into the temp table. Something like this data will appear in the temp table:
create table #tmp (objectID int, value1 char(1), value2 int) insert #tmp select 12 ,'R', 100 union all select 12 ,'R', 101 union all select 12 ,'S', 220 union all select 13 ,'D', 88 union all select 14 ,'K', 151 union all select 14 ,'K', 152
Then you can use this SQL batch, which you can put in the stored procedure, if necessary.
declare @sql nvarchar(max) select @sql = ISNULL(@sql+',','') + 'max(case when rn=' + cast(number as varchar) + ' then value1 end) value' + cast(number as varchar) + 'a,' + 'max(case when rn=' + cast(number as varchar) + ' then value2 end) value' + cast(number as varchar) + 'b' from master..spt_values where type='P' and number between 1 and ( select top 1 COUNT(*) from
Output
objectID value1a value1b value2a value2b value3a value3b ----------- ------- ----------- ------- ----------- ------- ----------- 12 R 100 R 101 S 220 13 D 88 NULL NULL NULL NULL 14 K 151 K 152 NULL NULL Warning: Null value is eliminated by an aggregate or other SET operation.
source share