How to stop a running query?

前端 未结 4 1750
被撕碎了的回忆
被撕碎了的回忆 2021-02-18 14:08

I use RODBC to send queries to an SQL-Server. Sometimes they take too much time to run, so I need to cancel them.

Clicking the red \"stop\" button in RStudio yields this

4条回答
  •  遇见更好的自我
    2021-02-18 14:38

    I've just stumbled upon the odbc package. It allows to interrupt a query at any time.

    Basic usage goes like this:

    library(DBI)
    
    myconnection <- dbConnect(odbc::odbc(),
                              driver = "SQL Server",
                              server = "my_server_IP_address",
                              database = "my_DB_name",
                              uid = "my_user_id",
                              pwd = "my_password")
    
    dbGetQuery(myconnection, myquery)
    

    I don't have a deep understanding of what happens behind the scenes, but for what I've seen so far in my personal use this package has other advantages over RODBC:

    • really faster
    • get the column types from the DB instead of guessing them (see here)
    • no stringsAsFactors and as.is arguments necessary

提交回复
热议问题