I am connecting to a local SQL Server database and running some queries in loop. The output from each iteration is saved as a RDS datafile locally on the disk.
This works fine when I execute this sequentially in standard for loop as shown below.
for(i in 1: NROW(Employee_Df))
{
df_results <- sqlQuery(myconn, paste(" SELECT * FROM Salary_Df
WHERE FullName like ",Employee_Df$FullName[i], "",sep="'"))
saveRDS(df_results,
file=paste0("/Home/Desktop", "Salary",Employee_Df$FullName[i], ".rds"),
compress = TRUE)
}
When I try to assign the output from the same query using foreach
, the code fails. I don't see an error messages but I don't see any valid RDS file saved based on the results from this query.
foreach(i = 1:2, .packages="RODBC")%dopar%{
df_results <- sqlQuery(myconn, paste(" SELECT * FROM Salary_Df
WHERE FullName like ",Employee_Df$FullName[i], "",sep="'"))
saveRDS(df_results,
file=paste0("/Home/Desktop", "Salary",Employee_Df$FullName[i], ".rds"),
compress = TRUE)
}
Any suggestions on how to make this work in foreach
is much appreciated.