• This is an excellent article by itself and even better that it suggests alternative solutions by other authors. I will try them all.

    When you need to do something quick, you normally use most familiar tools and if you don't have to run this query often, performance does not matter. I needed to get top 10 calls for each contact and pivot call dates to the output columns. Well, I used s cursor with the outer loop going from contact to contact and the inner loop moving from call to call. The line number for the call for a certain customer also served as a part of the column name in the update statement:

    select @strUpdate ='update ##TempTableCalls set Call_'+ convert(nvarchar(10),@LINE_NO) + ' = ' (the rest of the line going here)

    The temp table with the columns like Call_1, Call_2 was created in advance and populated with something additional before adding calls.

    After the update string for a call was composed, I used

    Exec sp_executesql @strUpdate

    Worked fine and reliable, I had to use it only twice, so not performance concerns, but I spent a lot of time writing it.

    Yelena

    Regards,Yelena Varsha