从R到SQL Server的奇怪连接行为

时间:2015-10-05 18:41:28

标签: sql-server r

当我尝试将RStudio环境与SQL Server连接时,我遇到了一个奇怪的问题。

案例1

我执行以下命令,它们可以正常工作:

dbhandleRSREPORTINGV1 <- odbcDriverConnect('driver={SQL Server};server=RS-REPORTING-V1;database=Loyalty;trusted_connection=true')

badData <- sqlQuery(dbhandleRSREPORTINGV1, paste("select * from loyalty where loyaltyplusstartdate = '10/1/2014'"))
goodData <- sqlQuery(dbhandleRSREPORTINGV1, paste("select * from loyalty where loyaltyplusstartdate <> '10/1/2014'"))

案例2

当我执行以下命令时,出现登录错误:

dbhandleRSREPORTINGV1 <- odbcDriverConnect('driver={SQL Server};server=RS-REPORTING-V1;database=Loyalty;trusted_connection=true')

goodDataSixMonthBeforeAfter <- sqlQuery(dbhandleRSREPORTINGV1, paste("select 
l.email,
(sum(ISNULL(o.Revenue,0))) as Revenue,
((DATEDIFF(day, l.loyaltyplusstartdate, o.created) + 179) / 30) - 6 as DeltaMonth
from [<linked_server>].<database_name>.dbo.ORDERS o
join (select distinct email,loyaltyplusstartdate,created from loyalty where loyaltyplusstartdate <> '10/1/2014' and loyaltyplusstartdate < '4/1/2015' group by email,loyaltyplusstartdate,created) l
on o.contactemail = l.email
where o.created
between DATEADD(day, -179, l.loyaltyplusstartdate) and DATEADD(day, 180, l.loyaltyplusstartdate)
group by 
l.email,l.loyaltyplusstartdate, 
((DATEDIFF(day, l.loyaltyplusstartdate, o.created) + 179) / 30) - 6"))

这是我得到的错误:

[1] "28000 18456 [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'NT AUTHORITY\\ANONYMOUS LOGON'."                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
[2] "[RODBC] ERROR: Could not SQLExecDirect 'select \nl.email,\n(sum(ISNULL(o.Revenue,0))) as Revenue,\n((DATEDIFF(day, l.loyaltyplusstartdate, o.created) + 179) / 30) - 6 as DeltaMonth\nfrom [<linked_server>].<database_name>.dbo.ORDERS o\njoin (select distinct email,loyaltyplusstartdate,created from loyalty where loyaltyplusstartdate <> '10/1/2014' and loyaltyplusstartdate < '4/1/2015' group by email,loyaltyplusstartdate,created) l\non o.contactemail = l.email\nwhere o.created\nbetween DATEADD(day, -179, l.loyaltyplusstartdate) and DATEADD(day, 180, l.loyaltyplusstartdate)\ngroup by \nl.email,l.loyaltyplusstartdate, \n((DATEDIFF(day, l.loyaltyplusstartdate, o.created) + 179) / 30) - 6'"

这很奇怪,因为我从案例1 更改为案例2 的所有内容都是查询。我已经单独执行了查询,它完全正常。那么为什么从RStudio尝试登录错误?

非常感谢任何帮助。

更新

根据@nrussell的建议,我执行了以下命令,但我仍然得到同样的错误:

goodDataSixMonthBeforeAfter <- sqlQuery(dbhandleRSREPORTINGV1, gsub("\\n", " ", paste("select 
l.email,
(sum(ISNULL(o.Revenue,0))) as Revenue,
((DATEDIFF(day, l.loyaltyplusstartdate, o.created) + 179) / 30) - 6 as DeltaMonth
from [10.24.50.182].OneLegalDataAnalysis.dbo.ORDERS o
join (select distinct email,loyaltyplusstartdate,created from loyalty where loyaltyplusstartdate <> '10/1/2014' and loyaltyplusstartdate < '4/1/2015' group by email,loyaltyplusstartdate,created) l
on o.contactemail = l.email
where o.created
between DATEADD(day, -179, l.loyaltyplusstartdate) and DATEADD(day, 180, l.loyaltyplusstartdate)
group by 
l.email,l.loyaltyplusstartdate, 
((DATEDIFF(day, l.loyaltyplusstartdate, o.created) + 179) / 30) - 6")))

0 个答案:

没有答案