Sql server 从R到SQL Server的奇怪连接行为
当我试图将我的Sql server 从R到SQL Server的奇怪连接行为,sql-server,r,Sql Server,R,当我试图将我的RStudio环境连接到sqlserver时,我遇到了一个奇怪的问题 案例1 我执行以下命令,它们通常工作正常: dbhandleRSREPORTINGV1 <- odbcDriverConnect('driver={SQL Server};server=RS-REPORTING-V1;database=Loyalty;trusted_connection=true') badData <- sqlQuery(dbhandleRSREPORTINGV1, paste(
RStudio
环境连接到sqlserver
时,我遇到了一个奇怪的问题
案例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'"))
最有可能是换行符引起了问题;请尝试使用
gsub(“\\n,”,粘贴(…)
。感谢您的建议,我已经更新了问题。我仍然会遇到相同的错误。过去我曾尝试在不同的数据库上执行类似的查询(多次),从RStudio
,我从未遇到过这样的问题(我从未使用过gsub
来替换\n
)。换句话说,我不认为“\n”会是一个问题。这对我来说很好:库(RODBC)数据库连接查看此以获取更多信息:
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'"
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")))