我在SQL Server中有一个存储过程,并且正在用R编写它。我在R中无法获得与该存储过程相同的输出。
ALTERProc [dbo].[UPD_CREDIT]
AS
BEGIN
DECLARE @cmd VARCHAR(1000)
DECLARE @ssispath VARCHAR(1000)
SET @ssispath = 'H:\RMS PKG\UPD_CREDIT.dtsx'
SELECT @cmd = 'dtexec /F "'+ @ssispath+'"'
SELECT @cmd = @cmd
exec master..xp_cmdshell @cmd
if exists(select top 2 *, task = (select TaskName from Table1 A
where A.id = B.id and A.taskid = B.taskid)
from Table2 B
where id = 5
and taskstatus = 'N'orderby taskrunID desc)
begin
RAISERROR('THERE IS AN ERROR IN EXECUTIoN ', 16, 1);
end
我的R函数是
UPD_CREDIT <- function(){
Table1 <- dbSendQuery(conn, 'Select * from Table1')
Table2 <- dbSendQuery(conn, 'Select * from Table2')
func1 <- function(Table1, Table2){
Table2 <- Table2 %>% filter(id = 5, taskstatus == 'N') %>% arrange(desc(taskrunID))
task <- merge(x = Table1,y = Table2 ,by =c("id","taskid"))
if (nrow(task) >2) {
res = TRUE }
res = FALSE
return(res) }
res <- func1(Table1, Table2)
if(res){print('THERE IS AN ERROR IN EXECUTIoN')}
}
我无法获得此部分,如何在R中执行这些操作
declare @cmdvarchar(1000)
declare @ssispathvarchar(1000)
set @ssispath='H:\RMS PKG\UPD_CREDIT.dtsx'
select @cmd='dtexec /F "'+ @ssispath+'"'
select @cmd= @cmd
execmaster..xp_cmdshell@cmd
答案 0 :(得分:0)
从本质上讲,这些行会将值传递到SSIS dtexec
utility的命令行shell调用中,R可以类似地与system()
运行。
这假定R在具有相同用户或特权的同一环境中运行(以访问H驱动器对象)以调用存储过程。该实用程序可能会更新同一连接数据库的 Table1 和 Table2 。另外,您可以使用merge
在数据库中直接运行JOIN
,并避免在客户端导入两个表。
# COMMAND LINE CALL
ssispath <- 'H:\\RMS PKG\\UPD_CREDIT.dtsx'
cmd <- paste0('dtexec /F "', @ssispath, '"')
system(cmd)
UPD_CREDIT <- function() {
# SQL SERVER QUERY
task <- dbSendQuery(conn, paste("SELECT *",
"FROM Table1 t1",
"INNER JOIN Table2 t2",
" ON t1.id = t2.id AND t1.taskid = t2.taskid",
" AND t2.id = 5 AND t2.taskstatus = 'N'")
)
if(nrow(task) > 2) print("THERE IS AN ERROR IN EXECUTION")
}