I have created an MS SQL 2012 data base using enterprise archiect and I am in the process of uploading data to this server using R and the dbi
and odbc
packages. Now, I have problem reading a table from my MS SQL 2012 sever, that I need to merge with another table that has a FK constraint to this table. What frustrates me is, that I can download the empty table, but not the filled table that I appended before.
Here is a picture of the EA data base modelling:
There is a main table "unternehmen" with information about firms that leads to a secondary table "eigentumsverhaeltnisse" with further information and one information is linked to a meta table "eigentuemer" containing the labels of this information. (I do not necessarily need this, but there are similar situations elsewhere). The entire data base (rougly 100 tables) is created using the DDL generation of enterprise architect. I could add more information on this, if needed.
So I created the meta table manually and uploaded it to the server using this code:
con <- DBI::dbConnect(odbc::odbc(),
Driver = "SQL Server",
Server = "MY server",
Database = "EA_DB",
encoding = "latin1")
df_temp<-data.table(bezeichnung=c("Land", … , "Staat"))
DBI::dbWriteTable(con, “eigentuemer”, df_temp, append=TRUE)
There is a bit more code to create the secondary table, which I then merge with the meta table after loading it from the SQL server to include the FK. This is then also uploaded using the same code as above.
df_temp <- code to create the other table
df_temp_sql<-DBI::dbReadTable(con, “eigentuemer”)
df_temp<-merge(df_temp,df_temp_sql,by="bezeichnung")
some other code
DBI::dbWriteTable(con, “eigentumsverhaeltnisse“, df_temp, append=TRUE)
Now I cannot reload the previously appended table, which is really frustrating and I was able to use the same command when the table was empty.
df_temp_sql<-DBI::dbReadTable(con, “eigentumsverhaeltnisse“,)
Error in result_fetch(res@ptr, n) :
nanodbc/nanodbc.cpp:2966: 07009: [Microsoft][ODBC SQL Server Driver]Ungültiger Deskriptorindex
I am assuming „ungültiger Deskriptorindex means invalid descriptor index. The table exists
DBI::dbExistsTable(con, "eigentumsverhaeltnisse")
I have found simliar questions, but I did not see a solution for me I tried different
df_temp_sql_4<-DBI::dbReadTable(con,DBI::SQL("eigentumsverhaeltnisse"))
df_temp_sql_5<-DBI::dbReadTable(con,"dbo.eigentumsverhaeltnisse")
df_temp_sql_6<-DBI::dbReadTable(con,DBI::SQL("dbo.eigentumsverhaeltnisse"))
I also tried dgGetQuery but get the same error
Bin <- DBI::dbGetQuery(con, "SELECT [id], [konzernname], [eigentuemer_id], [eigentuemer_andere],
[weitere_geschaeftsfelder], [konzernteil] FROM [EA_DB].[dbo].[eigentumsverhaeltnisse]")
Error in result_fetch(res@ptr, n) :
nanodbc/nanodbc.cpp:2966: 07009: [Microsoft][ODBC SQL Server Driver]Ungültiger Deskriptorindex
Similar questions are: Import Tables from SQL Server into R dbReadTable error in R: invalid object name dbReadTable error in R: invalid object name
Edit to answer comment: I do not quite understand the issue it the link. I do not have varchar(max) or varbinary(max) variable, do I? I have
PK id:bigint
konzername:ntext
FK eigentuemer_id:bigint
eigentuemer_andere:ntext
weitere_geschaftsfelder:bit
konzernteil:bit
What is strang is that some DBI::dbGetQuery
commands work when I only include a subset of the columns and some dont.
#Works
Bin <- DBI::dbGetQuery(con, "SELECT [id], [konzernname], [eigentuemer_andere] FROM [EA_DB].[dbo].[eigentumsverhaeltnisse]")
head(Bin)
#works as well
Bin <- DBI::dbGetQuery(con, "SELECT [id], [eigentuemer_id] FROM [EA_DB].[dbo].[eigentumsverhaeltnisse]")
head(Bin)
#Does not work
Bin <- DBI::dbGetQuery(con, "SELECT [id], [konzernname], [eigentuemer_andere], [eigentuemer_id] FROM [EA_DB].[dbo].[eigentumsverhaeltnisse]")
#works as well
Bin <- DBI::dbGetQuery(con, "SELECT [id], [weitere_geschaeftsfelder] FROM [EA_DB].[dbo].[eigentumsverhaeltnisse]")
head(Bin)
#Does not work
Bin <- DBI::dbGetQuery(con, "SELECT [id], [konzernname], [eigentuemer_andere], [weitere_geschaeftsfelder] FROM [EA_DB].[dbo].[eigentumsverhaeltnisse]")