mbayloon mbayloon - 4 months ago 43
Groovy Question

How can I call a stored procedure using groovy?


  1. how can i call a stored procedure using groovy?

  2. how can i create a stored procedure from grails project (as domain classes to create data base)?


Answer

An example of calling a FullName stored procedure which takes a param ('Sam' in the example) and returns a VARCHAR.

sql.call("{? = call FullName(?)}", [Sql.VARCHAR, 'Sam']) { name ->
    assert name == 'Sam Pullara'
}

The same example again but with a GString variation:

def first = 'Sam'
sql.call("{$Sql.VARCHAR = call FullName($first)}") { name ->
    assert name == 'Sam Pullara'
}

Here is an example of a stored procedure with an out parameter:

sql.call '{call Hemisphere(?, ?, ?)}', ['Guillaume', 'Laforge', Sql.VARCHAR], { dwells ->
    println dwells // => Northern Hemisphere
}

Refer this.