title: SQL Server: UPDATE after pivot (unpivot needed?)
body: <p>Two days ago I asked how to use pivot in SQL to transpose a table.</p>
  
  <p>With your help I got it right, here the solution: <a href="https://stackoverflow.com/questions/37483488/sql-server-transpose-rows-to-columns-nm-relationship">SQL server: Transpose Rows to Columns (n:m relationship)</a></p>
  
  <p>I'm running SQL Server 2012, with an OBDC-connection, programming in C#.</p>
  
  <p>So now I have:</p>
  
  <pre><code>| mitID | Name   | FamName | DOB        | abtIDref | HV            | PEV        | Drive      | Nex        |
  |-------|--------|---------|------------|----------|---------------|------------|------------|------------|
  | 1     | Frank  | Sinatra | 12.12.1915 | 1        | **30.5.2016** |            | 05.06.2015 | 02.11.2015 |
  | 2     | Robert | Downey  | 4.4.1965   | 2        | 27.7.2014     | 01.01.2016 | 20.01.2015 |            |
  </code></pre>
  
  <p>I get this with following SQL statement:</p>
  
  <pre><code>DECLARE @sql AS NVARCHAR(MAX)
  DECLARE @cols AS NVARCHAR(MAX)
  
  SELECT @cols = ISNULL(@cols + ',', '') + QUOTENAME(Descr)
  FROM Faehigkeiten ORDER BY faeID
  
  SET @sql = N'
      SELECT mitID, Name, FamName, DOB, abtIDref, ' + @cols + '
      FROM(
         SELECT mitID, Name, FamName, DOB, abtIDref, [when], descr
         FROM Mitarbeiter m
         JOIN[Link - List] l ON m.mitID = l.mitIDref
         JOIN Faehigkeiten f ON f.faeID = l.feaIDref
      ) a
      PIVOT(MAX([when]) FOR descr IN(' + @cols + ')) p'
  
  EXEC sp_executesql @sql
  </code></pre>
  
  <p>But when I enter a new date (shown in asterisk above), or change a name of a member, how do I save the data?</p>
  
  <p>I thought about <code>unpivot</code>, but I have no idea how to use it in my case.</p>
  
  <p>My solution would be to loop through all the rows and compare it to existing entries ... but that could take a long time with lots of members.</p>
  
  <p>Can you point me in some direction, wether it is possible to <code>UPDATE</code> this an easy way? I only need <code>UPDATE</code> no <code>INSERT</code> since new rows are forbidden.</p>
  
  <p>Right now I have no class for my members. I load my <code>DataGridView</code> in C# with the following code:</p>
  
  <pre><code>BindingSource bsMember = new BindingSource();
  bsMember.DataSource = DBMember.Get();
  dgv.DataSource = bsMember;
  </code></pre>
  
  <p>The <code>DBMember.Get()</code> returns a <code>DataTable</code> made with the above shown SQL code.</p>
  
  <p>Sorry, I have no approach except my loop through each row :/</p>
  
  <p>EDIT: The provided duplicate is hard for me to follow because of the <code>JOIN</code> in my SQL-code. Can i JOIN while updating?
  Please consider helping me directly.</p>