How to stop a running query? : <p>I use RODBC to send queries to an SQL-Server. Sometimes they take too much time to run, so I need to cancel them.</p>

<p>Clicking the red "stop" button in RStudio yields this error message:</p>

<blockquote>
  <p>R is not responding to your request to interrupt processing so to stop
  the current operation you may need to terminate R entirely.</p>
  
  <p>Terminating R will cause your R session to immediately abort. Active
  computations will be interrupted and unsaved source file changes and
  workspace objects will be discarded.</p>
  
  <p>Do you want to terminate R now?</p>
</blockquote>

<p>And if I click yes my session is indeed terminated. (note: using Rgui instead of RStudio doesn't make things better)</p>

<p>However:</p>

<ul>
<li><p>when I use another software (named "Query ExPlus") to connect to this same SQL-Server, I have a similar stop button, and clicking it instantly interrupts the query, without any crash.</p></li>
<li><p>when I connect to a PostgreSQL database using the RPostgres package I can also interrupt the query at any time.</p></li>
</ul>

<p>These two points lead me to think that there should be a way to solve my problem. What can I do?</p>

<p>So far my workaround is:</p>

<pre><code>library(RODBC)
library(R.utils)

withTimeout(mydf &lt;- sqlQuery(myconnection, myquery), timeout=120)
</code></pre>

<p><strong>Note:</strong> I don't have permission to kill queries from the database side.</p>
