R의 함수에서 저장 프로 시저를 호출하고 싶습니다. 아래 코드를 참조하십시오. 불행하게도이 코드는 값이없는 데이터 프레임 만 생성합니다. 이 문제를 RJDBC
& DBI
으로 수정하고 싶습니다. RODBC
에 문제가있는 것으로 보입니다.R, RJDBC를 사용하여 SQL Server 저장 프로 시저 호출
RPT_09_Hourly_Connected_v3<- function(Year, Month="NULL",State = "NULL",Region="NULL", City="NULL", District="NULL", Subdistrict="NULL" ,Address='NULL'){
drv <- JDBC("com.microsoft.sqlserver.jdbc.SQLServerDriver", "/opt/sqljdbc_3.0/sqljdbc4.jar")
conn <- DBI::dbConnect(drv, "jdbc:sqlserver://***;databaseName=***;user=***;password=***")
sqlText <- paste("exec [dbo].[RPT_09_Hourly_Connected_v3]@Year=",Year,
",@Month=",Month,
",@State=",State,"",
",@Region=",Region,"",
",@City=N'",City,"'",
",@District=",District,"",
",@Subdistrict=",Subdistrict,"",
",@Address=N'",Address,"'",
sep="")
data <- RJDBC::dbGetQuery(conn,sqlText)
}
a<- RPT_09_Hourly_Connected_v3(Year = 2016)
> str(a)
'data.frame': 0 obs. of 9 variables:
$ Regio : chr
$ Stad : chr
$ Stadsdeel : chr
$ Buurtcombinatie: chr
$ Adres : chr
$ Jaar : num
$ Maand : num
$ hourNR : num
$ HoursConnected : num
이것은 RODBC
이 작동하기 전에 작동했습니다. RODBC
과 RJDBC
사이에 어떤 차이가 있습니까? 나는 손으로 SQL Server의 저장 프로 시저를 실행하면
RPT_09_Hourly_Connected_v3<- function(Year, Month="NULL",State = "NULL",Region="NULL", City="NULL", District="NULL", Subdistrict="NULL" ,Address='NULL'){
dbhandle <- odbcConnect("***;DATABASE=***;UID=***;PWD=***")
data <- sqlQuery(dbhandle,paste("exec [ dbo].[RPT_09_Hourly_Connected_v3]@Year=",Year,
",@Month=",Month,
",@State=",State,"",
",@Region=",Region,"",
",@City=N'",City,"'",
",@District=",District,"",
",@Subdistrict=",Subdistrict,"",
",@Address=N'",Address,"'",
sep=""))
odbcCloseAll()
data
}
는 다음과 같이 표시됩니다
EXEC @return_value = [dbo].[RPT_09_Hourly_Connected_v3]
@Year = 2016,
@Month = NULL,
@State = NULL,
@Region = NULL,
@City = N'Amsterdam',
@District = NULL,
@Subdistrict = NULL,
@Address = NULL
감사를 읽어! 누군가가 나를 도울 수 있기를 바랍니다!.
마티
''@ City = N '', City, " '"'에 대한 줄에는'N '이 있어야합니까? 나는 SQL이 그 명령을'@ City = N'Detroit '처럼 읽으 려한다고 생각한다. 귀하의 코드가 오류를 반환하지 않는 데 나는 놀랍습니다. 주소 줄에 똑같은 것이 있습니다. 이게 내가 익숙하지 않은 변수 유형인가? – Benjamin
정확히 내가 SQL에 있기를 원했던 방식입니다. –