 <p>I believe this solves your problem.</p> <pre><code>DECLARE @command nvarchar(max) = 'SELECT CASE WHEN ( SELECT COUNT(*) FROM [table] WITH ( NOLOCK ) WHERE DATEDIFF(minute, systemupdatedtimestamp, GETDATE()) &lt; 10 ) &gt; 0 THEN 0 ELSE 1 END' exec sp_executesql @command </code></pre>