Hence, you need to iteratively run your sqlcmd for multiple SQL statements such as with lapply to return a list of dataframes, followed by an rbind call for single master dataframe: Id <- c(34, 22, 86)sqlcmd <- paste("select col1, col2 from DB where ItemId =", Id, sep="")# LIST OF DATAFRAMESdf_list <- lapply(sqlcmd , function(x) dbGetQuery(conn, x)) # FINAL DATAFRAMEfinal_df <- do.call(rbind, df_list)或者,对一个SQL语句使用UNION或UNION ALL. Alternatively, use a UNION or UNION ALL for one SQL statement. Id <- c(34, 22, 86)sqlcmd <- paste("select col1, col2 from DB where ItemId =", Id, sep="")single_sql <- paste(sqlcmd, collapse = " UNION ")final_df <- dbGetQuery(conn, single_sql)或者仍然使用OR:single_sql <- paste("select col1, col2 from DB where ItemId =", paste(Id, collapse=" OR ItemId = "))final_df <- dbGetQuery(conn, single_sql) 这篇关于带动态字符串的R dbGetQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-18 09:43