title: Filter records from an OpenQuery Result
body: <p>I am querying a linked server:</p>
  
  <pre><code>    DECLARE @result nvarchar(max);
      SET @Sql = N'
      SELECT * 
      FROM OPENQUERY(DATABASE_' + UPPER(@Environment) + ', ''
          Complex Query against the linked server...
      '');'
  
      EXECUTE sp_executesql @sql, @result OUTPUT
  </code></pre>
  
  <p>Before inserting the results into the correct table I have to filter some rows of the results. I can't filter them in the original query because the results must be matched against values in the target table.</p>
  
  <p>Something like:</p>
  
  <pre><code>INSERT INTO targetTable 
  SELECT * FROM @result AS r
  WHERE r.Col1 NOT IN (SELECT Col1 FROM targetTable)
  AND   r.Col2 NOT IN (SELECT Col2 FROM targetTable)
  </code></pre>
  
  <p>I can't use the <code>@results</code> variable to query from because it's no table.</p>
  
  <p>How can I do this?</p>