Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

Importing an entire SQL Server table into R


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use sqlcmd for bulk queries. It is far faster than anything Microsoft's ODBC drivers (or FreeTDS) can do.

If your query is (say)

SELECT Id, Field1, Field2 FROM TableName WHERE Field1 = 'QUUX'

Then your command line would be something like:

sqlcmd -S serveraddress -d databasename 
  -U username -P '*PASSWORD*' 
  -W -s , -o out.csv 
  -Q "SET NOCOUNT ON; SELECT Id, Field1, Field2 FROM TableName WHERE Field1 = 'QUUX'"

(If your server is on a non-standard port, then use -S serveraddress,portnumber.)

I tend to not like R's system (or system2, a thin veneer), instead preferring the processx package for its safer way of handling command line arguments.

bgquery <- processx::run("sqlcmd",
    args = c("-s", "serveraddress", "-d", "databasename",
             "-U", "username", "-P", "*PASSWORD*", "-W", "-s", ",", "-o", "out.csv",
             "-Q", "SET NOCOUNT ON; SELECT Id, Field1, Field2 FROM TableName WHERE Field1 = 'QUUX'"),
    error_on_status = FALSE, echo_cmd = FALSE, echo = FALSE)

# check its stdout, it can be rather verbose
tail(bgquery$results$stdout)

# check the return value, typically "0" for command success
bgquery$results$status

# if error, look at the error output, something might be in the stdout above, too
bgquery$results$stderr

At this point, read in the file:

x <- data.table::fread("out.csv", nrows = 3, na.strings = c("NA", "", "NULL"))

Note: sqlcmd does not quote the values. So for instance,

SELECT 'hello,world' as A

using sqlcmd will return

A
hello,world

which is obviously broken. If this is a problem, you may be stuck. bcp is another option with its own problems, so it's not a perfect replacement.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...