How to insert a dataframe into a SQL Server table?

后端 未结 3 558
梦毁少年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:50

    [edited] Perhaps pasting the names(df) would solve the scaling problem:

       values <- paste( " df[  , c(", 
                         paste( names(df),collapse=",") ,
                                       ")] ", collapse="" ) 
          values
          #[1] " df[  , c( a,b,c )] "
    

    You say your code is "working".. I would also have thought one would use sqlSave rather than sqlQuery if one wanted to "upload".

    I would have guessed this would be more likely to do what you described:

     sqlSave(con, df, tablename = "MyTable")
    

提交回复
热议问题