Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MS-SQL Bulk Insert with RODBC

Tags:

sql

r

rodbc

Is it possible to perform a bulk insert into an MS-SQL Server (2000, 2005, 2008) using the RODBC package?

I know that I can do this using freebcp, but I'm curious if the RODBC package implements this portion of the Microsoft SQL API and if not, how difficult it would be to implement it.

like image 882
ephpostfacto Avatar asked Sep 09 '09 20:09

ephpostfacto


3 Answers

check out the new odbc and DBI packages. DBI::dbWriteTable writes around 20,000 records per second... Much much faster than the Row Inserts from RODBC::sqlSave()

like image 145
quickreaction Avatar answered Oct 22 '22 07:10

quickreaction


Now You can use dbBulkCopy from the new rsqlserver package:

A typical scenario:

  1. You create a matrix
  2. you save it as a csv file
  3. You call dbBulkCopy to read fil and insert it using internally bcp tool of MS Sql server.

This assume that your table is already created in the data base:

dat <- matrix(round(rnorm(nrow*ncol),nrow,ncol)
id.file = "temp_file.csv"                      
write.csv(dat,file=id.file,row.names=FALSE)
dbBulkCopy(conn,'NEW_BP_TABLE',value=id.file)
like image 2
agstudy Avatar answered Oct 22 '22 06:10

agstudy


You're probably looking for ?sqlSave which uses a parametrized INSERT INTO query (taking place in one operation) when you set Fast=True.

like image 2
Tyler Avatar answered Oct 22 '22 07:10

Tyler