Skip to content

Instantly share code, notes, and snippets.

@rynmrtn
Created January 25, 2010 18:49
Show Gist options
  • Save rynmrtn/286114 to your computer and use it in GitHub Desktop.
Save rynmrtn/286114 to your computer and use it in GitHub Desktop.
Update Statement Generator
/*
This script will take a csv file (specified using the fileName variable) and
generate updates statements. The first row in the CSV file is assumed to be
the column names and all other rows are assumed to be data.
*/
// Setup basic information
tableName = "TABLE_NAME"
fileName = "C:\\file.csv"
i = 0
new File(fileName).splitEachLine(',') { fields ->
if(i++ == 0) {
columns = fields.collect { it }
} else {
values = fields.collect { it.equals("sysdate") ? it : ("'" + it + "'") }
len = fields.size()-1
print "UPDATE ${tableName} SET "
for(x in 1..len) {
if(x != 1) print ", "
print "${columns[x]}=${values[x]}"
}
print " WHERE ${columns[0]} = ${values[0]};"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment