I am unable to create the table using RODBC sqlSave (or, more precisely, writing data to the created table).
This is different from the existing sqlSave question / answer as
- the problems they encountered were different, I can create tables, while they could not and
- I have already misused their solutions, such as closing and reopening a connection before starting sqlSave, as well
- The error message is different, but the only exception is the message, which differs in two ways.
I am using MS SQL Server 2008 and 64-bit R on Windows RDP.
I have a simple data frame containing only 1 column consisting of 3, 4 or 5 digit integers.
> head(df) colname 1 564 2 4336 3 24810 4 26206 5 26433 6 26553
When I try to use sqlSave, no data is written to the table. In addition, the error message makes it sound as if the table could not be created, although the table is actually created using 0 rows.
Based on the assumption I found, I tried to close and reopen the RODBC connection right before starting sqlSave. Despite the fact that I use append = TRUE , I tried to reset the table before doing this, but nothing affected.
> sqlSave(db3, df, table = "[Jason].[dbo].[df]", append = TRUE, rownames = FALSE) Error in sqlSave(db3, df, table = "[Jason].[dbo].[df]", : 42S01 2714 [Microsoft][ODBC SQL Server Driver][SQL Server]There is already an object named 'df' in the database. [RODBC] ERROR: Could not SQLExecDirect 'CREATE TABLE [Jason].[dbo].[df] ("df" int)'
I also tried using sqlUpdate () in the table after creating it. It doesnโt matter if I create it in R or SQL Server Management Studio, I get a table not found on channel error message
Finally, note that I also tried this without append = TRUE and when creating a new table, as well as with and without the rownames option.
Mr.Flick from Freenode #R asked me to check if I can read in an empty table using sqlQuery, and indeed I can.
Update
I came a little closer to the following steps:
- I created an ODBC connection that is directly connected to my database in SQL Server, and not just the default database (Master), and then indicating the table path in the
table = or tablename = statements - Created a table in SQL Server Management Studio as follows
GO
CREATE TABLE [dbo].[testing123]( [Person_DIMKey] [int] NULL ) ON [PRIMARY]
GO
In R, I used sqlUpdate with my new ODBC connection and without parentheses around the tab name
Now sqlUpdate () sees the table, however it complains that it needs a unique column
Indicating that the only column in the table is a unique column with index = colname , an error occurs saying the column does not exist
I reset and recreate the table with the primary key,
GO
CREATE TABLE [dbo].[jive_BNR_Person_DIMKey]( [jive_BNR_Person_DIMKey] [int] NOT NULL PRIMARY KEY ) ON [PRIMARY]
GO
which generated both the primary key and the index (in accordance with the GUI SQL Sever Management Studio interface) with the name PK__jive_BNR__2754EC2E30F848ED
- I indicated this index / key as a unique column in sqlUpdate (), but I get the following error:
Error in sqlUpdate(db4, jive_BNR_Person_DIMKey, tablename = "jive_BNR_Person_DIMKey", : index column(s) PK__jive_BNR__2754EC2E30F848ED not in database table
For the record, I provided the correct column name (not "colname") for the index; thanks MrFlick for requesting clarifications.
In addition, these steps are numbered 1 through 7 in my post, but StackOverflow resets the list numbering several times when it is displayed. If someone can help me clear this aspect of this post, I would appreciate it.