共用方式為


StoredProcedure:SQL Server 預存程序:類別產生器

StoredProcedure:產生 SQLServer 預存程序物件和包含查詢的選擇性 .sql 檔案,以建立預存程序。 StoredProcedure$registrationVec 包含代表建立預存程序所需之查詢的字串

使用方式

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

引數

func

有效的 R 函數或有效的 R 函數字串名稱:1) 函數依賴的所有變數都應該在函數內定義,或以輸入參數的形式傳入。 輸入參數中最多可有 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 InputData 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)