StoredProcedure: SQL Server 저장 프로시저: 클래스 생성기

StoredProcedure: SQLServer 저장 프로시저 개체를 생성하고 필요에 따라 저장 프로시저를 만드는 쿼리를 포함하는 .sql 파일을 생성합니다. StoredProcedure$registrationVec는 저장 프로시저를 만드는 데 필요한 쿼리를 나타내는 문자열을 포함합니다.

사용

  StoredProcedure (func, spName, ..., filePath = NULL ,dbName = NULL,
  connectionString = NULL, batchSeparator = "GO")

인수

func

유효한 R 함수 또는 유효한 R 함수의 문자열 이름: 1) 함수가 사용하는 모든 변수는 함수 내부에 정의되거나 입력 매개 변수로 제공되어야 합니다. 입력 매개 변수 중에는 최대 하나의 데이터 프레임이 있을 수 있습니다. 2) 이 함수는 데이터 프레임, 명명된 목록 또는 NULL 중 하나를 반환해야 합니다. 목록 안에는 최대 하나의 데이터 프레임이 있을 수 있습니다.

spName

저장 프로시저의 이름을 지정하는 문자열입니다.

...

저장 프로시저의 선택적 입력 및 출력 매개 변수입니다. InputData, InputParameter 또는 outputParameter 클래스의 개체여야 합니다.

filePath

.sql을 만들 디렉터리의 경로를 지정하는 문자열입니다. NULL인 경우 .sql 파일이 생성되지 않습니다.

dbName

사용할 데이터베이스의 이름을 지정하는 문자열입니다.

connectionString

연결 문자열을 지정하는 문자열입니다.

batchSeparator

원하는 SQL 일괄 처리 구분 기호(filePath가 정의된 경우에만 해당)

SQLServer 저장 프로시저 개체


 ## Not run:

 ############# Example 1 #############
 # etl1 - reads from and write directly to the database
 etl1 <- function() {
   # The query to get the data
   qq <- "select top 10000 ArrDelay,CRSDepTime,DayOfWeek from AirlineDemoSmall"
   # The connection string
   conStr <- paste("Driver={ODBC Driver 13 for SQL Server};Server=.;Database=RevoTestDB;",
                 "Trusted_Connection=Yes;", sep = "")
   # The data source - retrieves the data from the database
   dsSqls <- RxSqlServerData(sqlQuery=qq, connectionString=conStr)
   # The destination data source
   dsSqls2 <- RxSqlServerData(table ="cleanData",  connectionString = conStr)
   # A transformation function
   transformFunc <- function(data) {
     data$CRSDepHour <- as.integer(trunc(data$CRSDepTime))
     return(data)
   }
   # The transformation variables
   transformVars <- c("CRSDepTime")
   rxDataStep(inData = dsSqls,
              outFile = dsSqls2,
              transformFunc=transformFunc,
              transformVars=transformVars,
              overwrite = TRUE)
   return(NULL)
 }
 # Create a StoredProcedure object
 sp_ds_ds <- StoredProcedure(etl1, "spTest",
                        filePath = ".", dbName ="RevoTestDB")
 # Define a connection string
 conStr <- paste("Driver={ODBC Driver 13 for SQL Server};Server=.;Database=RevoTestDB;",
                 "Trusted_Connection=Yes;", sep = "")
 # register the stored procedure with a database
 registerStoredProcedure(sp_ds_ds, conStr)
 # execute the stored procedure
 executeStoredProcedure(sp_ds_ds, connectionString = conStr)


 ############# Example 2 #############
 # train 1 takes a data frame with clean data and outputs a model
 train1 <- function(in_df) {
   in_df[,"DayOfWeek"] <- factor(in_df[,"DayOfWeek"], levels=c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"))
   # The model formula
   formula <- ArrDelay ~ CRSDepTime + DayOfWeek + CRSDepHour:DayOfWeek
   # Train the model
   rxSetComputeContext("local")
   mm <- rxLinMod(formula, data=in_df)
   mm <- rxSerializeModel(mm)
   return(list("mm" = mm))
 }
 # create InpuData Object for an input parameter that is a data frame
 # note: if the input parameter is not a data frame use InputParameter object
 id <- InputData(name = "in_df",
                defaultQuery = paste0("select top 10000 ArrDelay,CRSDepTime,",
                                      "DayOfWeek,CRSDepHour from cleanData"))
 # create an OutputParameter object for the variable inside the return list
 # note: if that variable is a data frame use OutputData object
 out <- OutputParameter("mm", "raw")

 # connections string
 conStr <- paste0("Driver={ODBC Driver 13 for SQL Server};Server=.;Database=RevoTestDB;",
                  "Trusted_Connection=Yes;")
 # create the stored procedure object
 sp_df_op <- StoredProcedure("train1", "spTest1", id, out,
                        filePath = ".")
 # register the stored procedure with the database
 registerStoredProcedure(sp_df_op, conStr)

 # get the linear model
 model <- executeStoredProcedure(sp_df_op, connectionString = conStr)
 mm <- rxUnserializeModel(model$params$op1)
## End(Not run)