查询在SQL Server中工作,但不在RODBC中

时间:2014-10-23 09:03:22

标签: sql-server r rodbc

我试图在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服务器功能已胜出&#39;在RODBC工作,所以生病了尝试更改查询(如果可能的话)。

我试图找到一些关于哪些SQL Server命令无法在RODBC中运行的信息,但没有成功。

2 个答案:

答案 0 :(得分:3)

我在ingres数据库上使用RJDBC时遇到了类似的问题,我认为原因是内存问题。

我在R中运行一个带有内部选择的大型查询(也会有大量数据)。它不起作用并返回错误,如

  

.verify.JDBC.result中的错误(r,&#34;无法检索&#34的JDBC结果集; ..

直接在数据库上运行相同的查询工作得很好(虽然花了一段时间)。起初我也认为它可能是内在选择。但是,我有一个内部选择的另一个查询工作正常,当我只在内部选择内部运行部分时它也不起作用。

我已经得出结论,这是一个内存问题,因为后来,当用户使用数据库的次数较少时,同样的查询确实从R运行(它仍然存在)。 当然不是RODBC或RJDBC不支持内部选择。我使用两种配置的内部选择工作,它们工作正常。

对我来说,这个查询有一个内部选择获得的特别大量的数据。而且我认为这就是为什么同时我可以运行另一个类似的查询,只使用较小的时间帧(因此内部选择获得的数据较少)。 希望这有助于找出问题所在。

答案 1 :(得分:0)

似乎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)