title: SQL Server : retrieve composite primary key after insert
body: <p>I need to retrieve a composite primary key from my table after inserting new rows.</p>
  
  <p>This is the table design:</p>
  
  <p><img src="https://i.stack.imgur.com/vQwVv.png" alt="enter image description here"></p>
  
  <p>This is my query</p>
  
  <pre><code>Set Nocount on
  
  insert into AE_Movimenta (NumContrato, ANoMov, NumMov, CodTpDoc, CodNatOper, CodCondPag, TipoMov, CodTab, CodTpEntrega, Tipo, DtMov, CodUsu, CodSetor, DtDoc, NumDoc, Serie, IdNfe, NumIP, DtSistema, CodSistema) 
  values (22, YEAR(GETDATE()), 15, 1,1,0,'S',0,1,'E',GETDATE(), 4, 1, GETDATE(), '', '','', '', GETDATE(), 1)
  
  select NumMov = @@identity
  
  set nocount off
  </code></pre>
  
  <p>And I get a <code>NULL</code>.</p>
  
  <p><img src="https://i.stack.imgur.com/PuSx8.png" alt="enter image description here"></p>
  
  <p>In this example, the <code>NumMov</code> primary key column is set manually, but I need to get this value dynamically after insert.</p>