I'm trying to append a dataframe to a sql server table using:
DBI::dbWriteTable(con_poc, "DEP_EVENTS", data_up, overwrite=FALSE, append = TRUE, verbose = TRUE, rownames = FALSE)
But I am getting an error on a column that is 'date' type in the database.
Error in result_insert_dataframe(rs@ptr, values) :
nanodbc.cpp:1587: 22003: [Microsoft][ODBC SQL Server Driver]Numeric value out of range
I previously formatted the column using as.POSIXct(example_date) but this only seems to work for 'datetime' columns
Can anyone help?
Adding info:
DEP_EVENTS:
DATA_REGION (varchar(50), not null)
EVENT_ID(PK, bigint, not null)
EVENT_NAME(varchar(200), not null)
FORECAST_STATUS(varchar(50), not null)
FORECAST_CYCLE(date, not null)
data_up <- data.frame(DATA_REGION = "America",
EVENT_NAME = "shiny deal",
FORECAST_STATUS = "Plan of Record",
FORECAST_CYCLE = as.Date("2017-07-07"))
DBI::dbWriteTable(con_poc, "DEP_EVENTS", data_up, overwrite=FALSE, append = TRUE, verbose = TRUE, rownames = FALSE)
Error in result_insert_dataframe(rs@ptr, values) :
nanodbc.cpp:1587: 22003: [Microsoft][ODBC SQL Server Driver]Numeric value out of range
I'm not inserting the primary key as I get the following error when I try that
Error in result_insert_dataframe(rs@ptr, values) :
nanodbc.cpp:1587: 23000: [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot insert explicit value for identity column in table 'DEP_EVENTS' when IDENTITY_INSERT is set to OFF.
Also as requested:
str(data_up)
'data.frame': 1 obs. of 4 variables:
$ DATA_REGION : Factor w/ 1 level "America": 1
$ EVENT_NAME : Factor w/ 1 level "shiny deal": 1
$ FORECAST_STATUS: Factor w/ 1 level "Plan of Record": 1
$ FORECAST_CYCLE : Date, format: "2017-07-07"
I also tried changing the factor columns to character but no change in the error.
format(example_date, format = "%Y-%m-%d")
? – HeinrickRODBCext
instead ofDBI
, and converting dates and datetimes to strings was required up until a recent release. I didn't know if it was similar inDBI
. – Heinrickas.Date()
? – Obscurationstr()
) and the SQL Server table's schema we would have gladly helped two days ago. Please provide those now. – Serrellas.POSIXct()
andas.Date()
but no luck with either yet – IgnaciaInvalid date format
if the problem is within the date column. Guessing around: Your columns get mixed up, and it fails inserting a string value into the EVENT_ID column. I would try: Remove the auto-generated primary key column, make it (for the test) a normal bigint column, try to insert all values of the table. Or the other way round: Remove all columns except the date column to verify the problem is really within the date column. As said: guessing around. – Surmisedata.frame()
try adding the arg,stringsAsFactors=False
as factors may be treated as numeric during database insert. Also, is Event_ID an auto-generated number, specifically Identity(1,1) field? – Serrellodbc
andRSQLServer
packages to connect to SQL Server, and it works fine in both cases. Which package are you using? Which operating system? Which version of SQL Server? – Millford