简体   繁体   English

从 MS SQL 2012 odbc 连接读取表时描述符索引无效

[英]Invalid Descriptor Index when reading table from MS SQL 2012 odbc connection

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.我已经使用 enterprise archiect 创建了一个 MS SQL 2012 数据库,我正在使用 R 以及dbiodbc包将数据上传到该服务器。 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.现在,我在从我的 MS SQL 2012 服务器读取一个表时遇到问题,我需要与另一个对该表具有 FK 约束的表合并。 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:这是 EA 数据库建模的图片: 在此处输入图像描述

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.有一个主表“unternehmen”,其中包含有关公司的信息,该表导致带有更多信息的辅助表“eigentumsverhaeltnisse”,并且一个信息链接到包含此信息标签的元表“eigentuemer”。 (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.整个数据库(大约 100 个表)是使用企业架构师的 DDL 生成创建的。 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.有更多的代码来创建辅助表,然后我在从 SQL 服务器加载它以包含 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.我假设 „ungültiger Deskriptorindex 表示无效的描述符索引。 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我也试过 dgGetQuery 但得到同样的错误

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类似的问题是: 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?我没有 varchar(max) 或 varbinary(max) 变量,是吗? 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.奇怪的是,当我只包含列的子集而有些不包含时,某些DBI::dbGetQuery命令可以工作。

#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]")

So that just to close this:这样就可以关闭它:

is is related to the isseu pointed out by @Hearkz https://github.com/r-dbi/odbc/issues/10 Which also relates to ntext and text variables, so that I need to adjust these variables to nvarchar(veryhighnumber) instead of nvarchar(MAX).与@Hearkz https://github.com/r-dbi/odbc/issues/10指出的isseu有关 这也与ntext和text变量有关,因此我需要将这些变量调整为nvarchar(veryhighnumber)而不是 nvarchar(MAX)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM