title: Using output to set a variable in a merge statement
body: <p>I have a merge statement that should update or insert a single record always. I want to remember the ID of that statement in a variable. It looks like this:</p>
  <pre><code>DECLARE @int int
  
  MERGE dbo.table AS A
  USING (SELECT 'stringtomatch' AS string) AS B ON B.string= A.string
  WHEN MATCHED THEN 
      UPDATE SET somecolumn = 'something'
  WHEN NOT MATCHED THEN
      INSERT 
      VALUES ('stringtomatch',
          'something')
  OUTPUT @int = inserted.ID;
  </code></pre>
  <p>Now this doesen't work because you can't set @int in the output clause this way. I know I could create a temptable and use <strong>INTO @temptable</strong> in the output. But since I <strong>know</strong> it's always a single record I want to have the ID in a INT variable.
  Is this even possible? Or am I forced to use a table variable.</p>
  <p>How?</p>