Forum Discussion

karun_r's avatar
karun_r
Microsoft Employee
8 years ago
Solved

R Script error when modifying the dataset- Missing Columns in data

I am trying to insert the dataset that I am receiving from another source into a SQL Server. As there is no option of doing this is native Power BI, I am using R functions to do this. Here is the code that I have:

 

library(RODBC);
serverConn <- odbcDriverConnect(connection="Driver={SQL Server Native Client 11.0};server=<Server>;database=<Database>;trusted_connection=yes;");
sqlSave(serverConn, dataset, tablename ="<tableName>",rownames=FALSE, append = TRUE)

 

When I run this script after loading the dataset, I am getting the below error:

 

DataSource.Error: ADO.NET: R script error.
Error in odbcUpdate(channel, query, mydata, coldata[m, ], test = test,  : 
  missing columns in 'data'
Calls: sqlSave -> sqlwrite -> odbcUpdate
Execution halted
Warning message:
closing unused RODBC handle 1 

Details:
    DataSourceKind=R
    DataSourcePath=R
    Message=R script error.
Error in odbcUpdate(channel, query, mydata, coldata[m, ], test = test,  : 
  missing columns in &#39;data&#39;
Calls: sqlSave -&gt; sqlwrite -&gt; odbcUpdate
Execution halted
Warning message:
closing unused RODBC handle 1 

    ErrorCode=-2147467259
    ExceptionType=Microsoft.PowerBI.Radio.RScriptRuntimeException

This error is coming at line 3. I have uncommented it and ran the step and it was working fine. Any idea why this is happening? This same code is running fine when I run it from R console.

  •  Figured it out. It was due to mismatch in the names of the columns. My dataframe has columns A and B but my table has columns C and B. Modified the columns to match the names and then it worked.

1 Reply

  • karun_r's avatar
    karun_r
    Microsoft Employee

     Figured it out. It was due to mismatch in the names of the columns. My dataframe has columns A and B but my table has columns C and B. Modified the columns to match the names and then it worked.