title: SQL "if exists..." dynamic query
body: <p>Suppose I have a query stored in a variable like this (it's actually dynamically populated and more complex, but this is for demonstration purposes):</p>
  
  <pre><code>DECLARE @Query VARCHAR(1000) = 'SELECT * FROM dbo.MyTable'
  </code></pre>
  
  <p>Is there a way to check if the query would return any results? Something like this, but this doesn't work:</p>
  
  <pre><code>IF EXISTS (@Query)
  BEGIN
      -- do something
  END
  </code></pre>
  
  <p>The only way that I can think of to do this is to put the results in a temp table and then query from that, but that is not ideal because the columns in the dynamic query can vary and I really don't need the temp table at all for any reason other than checking whether some rows would be returned. Is there a better way?</p>