RODBC中临时表的问题

时间:2016-08-08 16:16:14

标签: r rodbc

我有下面的代码,我试图用它来创建一个带有日期时间戳的表,一年中的每一分钟,然后离开连接到另一个表,最后使用一些tsql在最终选择中创建一个新字段返回的语句。 sql在sql server中运行得很好,但是当我尝试在RODBC中运行它时,我得到错误,空字符向量或带有sql server错误消息的字符向量。我已经看过一些关于在RODBC中使用临时表并用“go”分隔它们或者分离sqlQuery语句的帖子。我尝试用“go”分离步骤失败了,我不确定将语句分成几个sqlQuery语句会在这种情况下工作,因为我想在最终的select语句中使用两个临时表和一些tsql函数。非常感谢任何建议。

Code:

library("forecast")
library("tseries")
library("sqldf")
library("manipulate")
library("caret")
library("qdapTools")
library("RODBC")
library("dplyr")
library("xts")




con<-odbcConnect("RsqlConnect")

dfETest<-sqlQuery(con,"

            create table #TempFM (dt datetime not null);
            declare @datestart datetime = '2015-01-01 00:00', @dateend datetime = '2015-12-31 23:59';

            while @datestart <= @dateend
            begin
            insert into #TempFM (dt)
            select dateadd(minute, number, @datestart)
            from master..spt_values 
            where
            type = 'p' and number between 0 and 1439
            and dateadd(minute, number, @datestart) <= @dateend;
            set @datestart = dateadd(minute, 1440, @datestart);
            end 


            select 
            dTime,                

            1 as CV

            into #TempP
            from TableM


            select DATEADD(hh,datepart(hour,dt), DATEADD(mi,(datepart(minute,dt)/15)*15,cast(cast(dt as DATE)as datetime))) as VTQ, 
            *
            from #TempFM f
            left join #TempP p on p.dTime = f.dt
            ")


Data:

dput(TableM[1:100,])
structure(list(dTime = structure(c(1435505100, 1424580900, 1423598220, 
1426616460, 1440028200, 1449095520, 1426370640, 1423697880, 1440517860, 
1426479960, 1428707940, 1450308780, 1449771840, 1437770880, 1433912820, 
1449501900, 1427121780, 1430257500, 1446993480, 1440373200, 1432256100, 
1426721700, 1449449340, 1422559320, 1423162920, 1422683580, 1423545300, 
1424242200, 1445704920, 1447931340, 1443834480, 1450638300, 1446287220, 
1451246520, 1449158280, 1447267260, 1448101560, 1449361860, 1450958520, 
1447351680, 1448352120, 1449038040, 1447534680, 1448908620, 1440340980, 
1443400980, 1448952120, 1448851680, 1448856540, 1447201440, 1448128440, 
1448061660, 1438615980, 1444851960, 1447698180, 1450836060, 1451323080, 
1447693140, 1447981980, 1440027300, 1449005220, 1446873960, 1451392260, 
1449064500, 1450879140, 1451177100, 1450883100, 1449081840, 1448165040, 
1443192720, 1442772900, 1447801140, 1444959300, 1446919980, 1447376220, 
1447702980, 1450558080, 1450360620, 1443759360, 1440593340, 1450672440, 
1441950840, 1449247140, 1447280580, 1449704160, 1451545800, 1445292540, 
1449862440, 1450046580, 1449707520, 1450376520, 1450904100, 1447016700, 
1445739720, 1449717180, 1445973000, 1438405920, 1450006500, 1443343800, 
1449465540), class = c("POSIXct", "POSIXt"), tzone = ""), CV = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L)), .Names = c("dTime", "CV"), row.names = c(NA, 
100L), class = "data.frame")

0 个答案:

没有答案