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