Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't RODBC upload a dataframe to SQL Server?

Tags:

r

rodbc

library(RODBC)
con <- odbcDriverConnect("driver=SQL Server; server=name")
df <- data.frame(a=1:10, b=10:1, c=11:20)

Trying to upload the dataframe:

sqlSave(con, df, tablename='[MyDatabase].[MySchema].[MyTable]', rownames=F)

>Error in sqlColumns(channel, tablename) : ‘MyDatabase.MySchema.MyTable’: table not found on channel

..alternatively creating the table first and then appending to it:

cmd <- "create table [MyDatabase].[MySchema].[MyTable] ([a]  int, [b] int, [c] int)"
sqlQuery(con, cmd)

sqlSave(con, df, tablename='[MyDatabase].[MySchema].[MyTable]', rownames=F, append=T)

>Error in sqlSave(con, df, tablename = "MyTable", rownames = F, : 42S01 2714 [Microsoft][ODBC SQL Server Driver][SQL Server]There is already an object named MyDatabase.MySchema.MyTable in the database. [RODBC] ERROR: Could not SQLExecDirect 'CREATE TABLE MyDatabase.MySchema.MyTable ("a" int, "b" int, "c" int)'

What am I doing wrong?

like image 609
jenswirf Avatar asked Jan 16 '13 09:01

jenswirf


1 Answers

If I add brackets I also get an error.

If I use a connection string with the database to make sure that I am in the correct database (not master) and execute the statement sqlSave(con, df, tablename='dbo.MyTable4', rownames=F) or sqlSave(con, df, tablename='MyTable5', rownames=F) it works.

like image 82
Henrico Avatar answered Nov 10 '22 10:11

Henrico