Skip to content

Instantly share code, notes, and snippets.

@fabito
Created April 9, 2011 19:24
Show Gist options
  • Save fabito/911692 to your computer and use it in GitHub Desktop.
Save fabito/911692 to your computer and use it in GitHub Desktop.
Generating CSV files using sqlcmd and groovy
def username = args[0]
def password = args[1]
def host = args[2]
def database = args[3]
def dir = './'
def ant = new AntBuilder()
def p = ~/.*\.sql/
new File( dir ).eachFileMatch(p) { f ->
def sqlFile = f.name
def tempCsvFile = sqlFile.replaceAll(/.sql/,'')
def cmd = "sqlcmd -S $host -U $username -P $password -d ${database} -i ${sqlFile} -W -o ${tempCsvFile}.temp -s ;"
def process = cmd.execute()
process.waitFor()
ant.move(file: "${tempCsvFile}.temp", tofile:"${tempCsvFile}.csv", overwrite: true ) {
filterchain(){
headfilter(lines:"-1", skip: "2")
tailfilter(lines: "-1", skip: "2" )
ignoreblank()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment