Skip to content

Instantly share code, notes, and snippets.

@aadouglass
Created April 3, 2019 19:03
Show Gist options
  • Save aadouglass/09ce2e3938c5a5683d46133338213db5 to your computer and use it in GitHub Desktop.
Save aadouglass/09ce2e3938c5a5683d46133338213db5 to your computer and use it in GitHub Desktop.
df.sqlInsert <- function(dataframe) {
count <- 1
query_start <- "INSERT INTO EpicorReports.db_ddladmin.TempVP ("
while (count <= ncol(dataframe)) {
if (ncol(dataframe) > count) {
query_start <- paste0(query_start, colnames(dataframe[count]), ", ")
}
if (ncol(dataframe) == count) {
query_start <- paste0(query_start, colnames(dataframe[count]), ") VALUES (")
}
count <- count + 1
}
count <- 1
while (count <= nrow(dataframe)) {
if (nrow(dataframe)+1 > count) {
subcount <- 1
while (ncol(dataframe) > subcount) {
query_start <- paste0(query_start, "'", dataframe[count, subcount], "', ")
subcount <- subcount + 1
}
ifelse(count == nrow(dataframe) && ncol(dataframe) == subcount,
query_start <- paste0(query_start, "'", dataframe[count, subcount], "')"),
query_start <- paste0(query_start, "'", dataframe[count, subcount], "'),("))
}
count <- count + 1
}
sqlQuery(con1,query_start)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment