Вопрос

Does this make sense? I otherwise don't see the error.

Using RODBC, R returns a 'Could not SQLExecDirect' error for a sqlQuery statement issued to a table containing a field ID = Order. The SQL otherwise works. I can however read the entire table to a df using sqlFetch (see below).

The target db is on SQL Server.

Example of table structure:

Taxon_Id = c(3000,3001,3002)
Group_Id = c(6,5,5)
Type = c('Fish','Fish','Fish')
Order = c('Petromyzontidae','Acipenseridae','Clupeidae')
Family = c('Petromyzontidae','Acipenseridae','Clupeidae')
txn = data.frame(Taxon_Id,Group_Id,Type,Order,Family)

Example of SQL issued to table:

txn2<-as.data.frame(sqlQuery(channel, paste('SELECT T.Taxon_Id,
           T.GroupId,
           T.Type,
           T.Order,
           T.Family
     FROM Taxon T
     ORDER BY 1

     '))   )

sqlFetch reads all table fields without error.

txn<-as.data.frame(sqlFetch(channel,"Taxon"))

Thanks for your comments.

Это было полезно?

Решение

This is your query:

SELECT T.Taxon_Id, T.GroupId, T.Type, T.Order, T.Family
FROM Taxon T
ORDER BY 1

In SQL (in general) and SQL Server (in particular), the word Order is a reserved word. You need to escape it with either double quotes or square braces:

SELECT T.Taxon_Id, T.GroupId, T.Type, T.[Order], T.Family
FROM Taxon T
ORDER BY 1
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top