sqlSave: Mapping dataframe timestamps to SQL Server timestamps

前端 未结 1 1953
南旧
南旧 2021-01-01 23:42

I am trying to upload a data frame to a table in sql server using sqlSave(). This dataframe has a timestamp in it and I\'d like to map the timestamp col to

相关标签:
1条回答
  • Two options:

    1) Lazy one: let the error occur, the table will be created, and change the column(s) to datetime manually in your database. It will work the next time.

    2) Correct: use varTypes

    Note that your problem can be stripped down by removing unnecessary stuff. As an aside, I probably would not use the column name timestamp in an sql server, because I have seen confusions because of the internal timestamp data type is totally different.

    library(RODBC)
    mdf = data.frame(timestamp=as.POSIXct(Sys.time()))
    
    varTypes = c(timestamp="datetime")
    channel = odbcConnect("test")
    sqlSave(channel,mdf,rownames=FALSE,append=TRUE,varTypes=varTypes)
    close(channel)
    
    0 讨论(0)
提交回复
热议问题