I am working in a previously existing grails project that has some search functionality built into it. It has created a new Sql object using my SQL Server datasource, and it seems like it is attempting to call a stored procedure like so:
def qResults = sql.rows(spCall)
where spCall is a String and looks like this:
EmployeeQueryClient 'SomeClient', 1,1,0
Where “EmployeeQueryClient is the name of the stored procedure, and the other things are the parameters.
I can’t find any documentation supporting this kind of call – is this correct? How would I really do this if it is not?
In the code shown above
sqlis an instance of groovy.sql.Sql. This provides arows(String sql)method that can be used to execute SQL and returns the result.Generally speaking, I think this
rowsmethod is a bad choice if you want to call a stored proc, because you have to concatenate the name of the proc and all the args into a single string, which is a bad idea from the point of view of both type safety and readability.Instead use one of the overload
callmethods provided by the same class, which are specifically intended for invoking stored procedures. For example, if you just want to invoke the procedure (ignoring any results it returns) use:The syntax used here for calling a stored proc,
will work for MySql. If you’re not using MySql you’ll need to replace this with whatever is used by your RDBMS to invoke a stored proc.
Overloaded versions of the
callmethod enable you to handle any results returned and/or ouput parameters of the stored proc.