Skip to content

Instantly share code, notes, and snippets.

@joeRinehart
Created February 6, 2015 15:27
Show Gist options
  • Save joeRinehart/b1decb087673d5423a76 to your computer and use it in GitHub Desktop.
Save joeRinehart/b1decb087673d5423a76 to your computer and use it in GitHub Desktop.
/*
Adds a SQL-server specific callRows() method to Groovy Sql that functions like rows() against stored
procedures that perform DML before doing a select.
Essentially, it's just a set nocount on/off wrapper.
Usage:
sql.callRows(
"someProcName",
[
paramOne: 'valOne',
paramTwo: 'valTwo',
]
)
*/
Sql.metaClass.callRows << { String procName, Map params ->
// build a statement
String statement = "{call ${procName}("
params.eachWithIndex { k, v, i ->
statement+= i>0 ? ',' : '' + ':' + k
}
statement += ')}'
// do the wrapped call
def result
sql.withTransaction {
sql.execute('set nocount on')
result = sql.rows(statement, params)
sql.execute('set nocount off')
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment