Dynamic Pivot in SQL Server

I you have a table F1,




And table 2 like Child. Then the pivot query will be,
  1. declare@ pp varchar(max)  
  2. declare@ pp1 varchar(max)  
  3. declare@ pivot varchar(max)  
  4. create table pivot_columns(pivot_column varchar(100))  
  5. set@ pp = ' insert into pivot_columns select distinct Cid from child'  
  6. exec(@pp)  
  7.     --set@ sql1 = 'select pivot_column from pivot_columns'  
  8. select@ pivot = coalesce(@pivot + ',''') + '[' + pivot_column + ']'  
  9. from pivot_columns  
  10. set@ pp1 = 'Select * from ( Select Pid,Cid,Name from Child) as SourceTable Pivot (max(name) for CId in (' + @pivot + ')) as PivotTable'  
  11. drop table pivot_columns  
  12. print(@pp1)