RODBC sqlSave table creation problems

前端 未结 6 2059

I\'m having trouble creating a table using RODBC\'s sqlSave (or, more accurately, writing data to the created table).

This is different than the existing sqlSave qu

6条回答
  •  别那么骄傲
    2020-12-15 12:16

    In addition to some of the answered posted earlier, here's my workaround. NOTE: I use this as part of a small ETL process, and the destination table in the DB is dropped and recreated each time.

    Basically you want to name your dataframe what you destination table is named:

    RodbcTest <- read.xlsx('test.xlsx', sheet = 4, startRow = 1, colNames = TRUE, skipEmptyRows = TRUE)
    

    Then make sure your connection string includes the target database (not just server):

    conn <- odbcDriverConnect(paste("DRIVER={SQL Server};Server=localhost\\sqlexpress;Database=Charter;Trusted_Connection=TRUE"))
    

    after that, I run a simple sqlQuery that conditionally drops the table if it exists:

    sqlQuery(conn, "IF OBJECT_ID('Charter.dbo.RodbcTest') IS NOT NULL DROP TABLE Charter.dbo.RodbcTest;")
    

    Then finally, run sqlSave without the tablename param, which will create the table and populate it with your dataframe:

    sqlSave(conn, RodbcTest, safer = FALSE, fast = TRUE)
    

提交回复
热议问题