查询在 SQL Server 中工作,但在 RODBC 中不工作
Posted
技术标签:
【中文标题】查询在 SQL Server 中工作,但在 RODBC 中不工作【英文标题】:Query working in SQL server, but not in RODBC 【发布时间】:2014-10-23 09:03:21 【问题描述】:我正在尝试在RODBC
中运行以下查询:
library(RODBC)
sql <- paste("SELECT * INTO #DataQuery FROM ( SELECT * FROM [DSMM_PPCreporting].[dbo].[DSMM_FACT_TABLE] WITH (NOLOCK) WHERE [DSMM_FACT_TABLE].Client_ID = 85 AND VenueSource=1 AND TimeStamp BETWEEN '10/1/2014' AND '10/13/2014' AND Venue_ID in (1,14)) xxxx")
sql <- paste(sql, "SELECT Timestamp as Date, isnull(FlexCategory.FlexCategoryName,'UNASSIGNED') AS [Flex Category], isnull(Venue.Venue,'UNASSIGNED') AS [Venue], isnull(CategoryName,'UNASSIGNED') AS [Campaign], isnull(Device,'UNASSIGNED') AS [Device], ")
sql <- paste(sql, "round(SUM(isNull(cost,0)),2) AS [Cost ($)],SUM(isNull(VenueConversions1PC,0)) +SUM(isNull(VenueConversions,0)) AS [Venue Leads] ")
sql <- paste(sql, "INTO #FlexCategory FROM #DataQuery DSMM_FACT_TABLE WITH (NOLOCK) ")
sql <- paste(sql, "LEFT JOIN Device WITH (NOLOCK) ON DSMM_FACT_TABLE.Device_ID = Device.Device_ID")
sql <- paste(sql, "LEFT JOIN Venue WITH (NOLOCK) ON DSMM_FACT_TABLE.Venue_ID = Venue.Venue_ID")
sql <- paste(sql, "LEFT JOIN Category WITH (NOLOCK) ON DSMM_FACT_TABLE.Category_ID = Category.Category_ID")
sql <- paste(sql, "LEFT JOIN FlexCategory WITH (NOLOCK) ON Category.FlexCategory_ID = FlexCategory.FlexCategory_ID")
sql <- paste(sql, "GROUP BY Timestamp, isnull(FlexCategory.FlexCategoryName,'UNASSIGNED') ,")
sql <- paste(sql, "isnull(CategoryName,'UNASSIGNED'), ")
sql <- paste(sql, "isnull(Venue.Venue,'UNASSIGNED'),")
sql <- paste(sql, " isnull(Device,'UNASSIGNED')")
sql <- paste(sql, "ORDER BY [Venue Leads] DESC")
sql <- paste(sql, "SELECT * FROM #FlexCategory WITH (NOLOCK)")
##sql <- paste(sql, "DROP TABLE #FlexCategory")
##sql <- paste(sql, "DROP TABLE #DataQuery")
# Connecting R to DSMM and running the SQL query there
connect <- odbcConnect("DSMM")
system.time(Data <- sqlQuery(connect, sql))
close(connect)
此查询在 Microsoft SQL Server 中完美运行:
SELECT * INTO #DataQuery FROM ( SELECT * FROM [DSMM_PPCreporting].[dbo].[DSMM_FACT_TABLE] WITH (NOLOCK) WHERE [DSMM_FACT_TABLE].Client_ID = 85 AND VenueSource=1 AND TimeStamp BETWEEN '10/1/2014' AND '10/13/2014' AND Venue_ID in (1,14)) xxxx SELECT Timestamp as Date, isnull(FlexCategory.FlexCategoryName,'UNASSIGNED') AS [Flex Category], isnull(Venue.Venue,'UNASSIGNED') AS [Venue], isnull(CategoryName,'UNASSIGNED') AS [Campaign], isnull(Device,'UNASSIGNED') AS [Device], round(SUM(isNull(cost,0)),2) AS [Cost ($)],SUM(isNull(VenueConversions1PC,0)) +SUM(isNull(VenueConversions,0)) AS [Venue Leads] INTO #FlexCategory FROM #DataQuery DSMM_FACT_TABLE WITH (NOLOCK) LEFT JOIN Device WITH (NOLOCK) ON DSMM_FACT_TABLE.Device_ID = Device.Device_ID LEFT JOIN Venue WITH (NOLOCK) ON DSMM_FACT_TABLE.Venue_ID = Venue.Venue_ID LEFT JOIN Category WITH (NOLOCK) ON DSMM_FACT_TABLE.Category_ID = Category.Category_ID LEFT JOIN FlexCategory WITH (NOLOCK) ON Category.FlexCategory_ID = FlexCategory.FlexCategory_ID GROUP BY Timestamp, isnull(FlexCategory.FlexCategoryName,'UNASSIGNED') , isnull(CategoryName,'UNASSIGNED'), isnull(Venue.Venue,'UNASSIGNED'), isnull(Device,'UNASSIGNED') ORDER BY [Venue Leads] DESC SELECT * FROM #FlexCategory WITH (NOLOCK)
我知道我的 SQL 连接运行良好,并且我使用的另一个查询运行良好,例如以下代码在 R 中运行良好,并将数据框加载到全局环境中:
sql <- "SELECT TK_ID, Client_ID, tk.searchterm_id, SearchTerm, RegionName, CityName, Timestamp FROM [DSMM_PPCreporting].[dbo].[tk] "
sql <- paste(sql, "left join [DSMM_PPCreporting].[dbo].searchterm (nolock) on [DSMM_PPCreporting].[dbo].tk.searchterm_id=[DSMM_PPCreporting].[dbo].searchterm.searchterm_id ")
sql <- paste(sql, "left join [DSMM_PPCreporting].[dbo].Region (nolock) on [DSMM_PPCreporting].[dbo].tk.Region_ID=[DSMM_PPCreporting].[dbo].Region.Region_ID ")
sql <- paste(sql, "left join [DSMM_PPCreporting].[dbo].City (nolock) on [DSMM_PPCreporting].[dbo].tk.city_id=[DSMM_PPCreporting].[dbo].city.city_ID ")
#sql <- paste(sql, "where convert(Date, [LastUpdate]) = dateadd(day, datediff(day, 1, GETDATE()), 0)")
sql <- paste(sql, "where convert(Date, [LastUpdate]) >= '2014-10-15' and convert(Date, [LastUpdate]) <= dateadd(day, datediff(day, 1, GETDATE()), 0)")
# Connecting R to DSMM and running the SQL query there
connect <- odbcConnect("DSMM")
system.time(Data <- sqlQuery(connect, sql))
close(connect)
我知道一些SQL server
命令在RODBC
中不起作用,这可能是问题吗?如果是这样,有人可以向我指出哪些SQL 服务器函数在RODBC
中不起作用,所以生病尝试更改查询(如果可能)。
我试图找到一些关于哪些 SQL Server 命令在 RODBC
中不起作用的信息,但没有任何成功。
【问题讨论】:
【参考方案1】:我在 ingres 数据库上使用 RJDBC 时遇到了类似的问题,我认为原因是内存问题。
我正在使用内部选择从 R 中运行一个大型查询(这也会有大量数据)。它不起作用并返回类似
的错误.verify.JDBC.result(r, "Unable to retrieve JDBC result set for "..中的错误..
直接在数据库上运行相同的查询工作得非常好(虽然花了一段时间)。起初我还认为它可能是内部选择。但是,我有另一个带有内部选择的查询,它工作正常,当我只运行内部选择中的部分时,它也不起作用。
我断定这是内存问题,因为后来,当使用数据库的用户减少时,确实从 R 运行了相同的查询(现在仍然如此)。 当然不是 RODBC 或 RJDBC 不支持内部选择。我已经使用了两种配置的内部选择,它们工作正常。
对我来说,这个查询通过内部选择获得了特别大量的数据。我认为这就是为什么我可以同时运行另一个类似的查询,只使用更小的时间范围(因此内部选择获得的数据更少)。 希望这有助于找出问题所在。
【讨论】:
听起来很有趣,有时间再看看,哇,这是一个旧的:)【参考方案2】:似乎RODBC
不支持内部选择语句:
此代码有效:
sql <- paste("SELECT TimeStamp FROM [DSMM_PPCreporting].[dbo].[DSMM_FACT_TABLE] WHERE [DSMM_FACT_TABLE].Client_ID = 85 AND VenueSource=1 AND TimeStamp BETWEEN '10/1/2014' AND '10/13/2014' AND Venue_ID in (1,14)")
connect <- odbcConnect("DSMM")
Data <- sqlQuery(connect, sql)
close(connect)
虽然不是这样:
sql <- paste("SELECT * FROM (SELECT TimeStamp FROM [DSMM_PPCreporting].[dbo].[DSMM_FACT_TABLE] WHERE [DSMM_FACT_TABLE].Client_ID = 85 AND VenueSource=1 AND TimeStamp BETWEEN '10/1/2014' AND '10/13/2014' AND Venue_ID in (1,14))")
connect <- odbcConnect("DSMM")
Data <- sqlQuery(connect, sql)
close(connect)
【讨论】:
以上是关于查询在 SQL Server 中工作,但在 RODBC 中不工作的主要内容,如果未能解决你的问题,请参考以下文章
Laravel 查询构建器 - 查询不工作但在 SQL 控制台中工作
SQL Server 查询因 RODBC 连接而失败,在 SQL Server 中工作
MySql 查询在 PHPmyadmin 中工作,但在 codeigniter 中不工作