How to insert a dataframe into a SQL Server table?

后端 未结 3 567
梦毁少年i
梦毁少年i 2020-12-05 05:29

I\'m trying to upload a dataframe to a SQL Server table, I tried breaking it down to a simple SQL query string..

library(RODBC)
con <- odbcDriverConnect(\         


        
3条回答
  •  广开言路
    2020-12-05 05:48

    Since insert INTO is limited to 1000 rows, you can dbBulkCopy from rsqlserver package.

    dbBulkCopy is a DBI extension that interfaces the Microsoft SQL Server popular command-line utility named bcp to quickly bulk copying large files into table. For example:

    url = "Server=localhost;Database=TEST_RSQLSERVER;Trusted_Connection=True;"
    conn <- dbConnect('SqlServer',url=url)
    ## I assume the table already exist
    dbBulkCopy(conn,name='T_BULKCOPY',value=df,overwrite=TRUE)
    dbDisconnect(conn)
    

提交回复
热议问题