Quantcast

Can't return a value from a stored procedure

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Can't return a value from a stored procedure

Kenneth Kousen
I'm trying to call a stored procedure by injecting the dataSource into a service, and though the call is working, I can't seem to return the result.

As an example, the MySQL sample database called Sakila has a stored procedure called film_in_stock, whose signature is:

film_in_stock(IN p_film_id INT, IN p_store_id INT, OUT p_film_count INT)

So I made a service class:

class DataService {
  def dataSource

  def filmInStock(filmId, storeId) {
    Sql db = new Sql(dataSource)
    sql.call('{call film_in_stock(?,?,?)}', [filmId, storeId, Sql.INTEGER]) { count ->
      println count
      return count
    }
  }
}

I called this from a controller method:

class MyController {
  def dataService

  def callProc() {
    render dataService.filmInStock(1,1)
  }
}

The result of accessing the controller action callProc is that 4 gets printed to the console (as it should), but the procedure returns null, which is what is rendered.

Why can I print the value but not return it? Am I missing something obvious?

Thanks,

Ken
--
Kenneth A. Kousen
President
Kousen IT, Inc.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Can't return a value from a stored procedure

zyro
i think the return returns only from the closure which is the last
argument for sql.call

sth. like this should work:

def myCount
sql.call(...) { count ->
    myCount = count
}
return myCount

zyro

-------- Original Message  --------
Subject: [grails-user] Can't return a value from a stored procedure
From: Kenneth Kousen <[hidden email]>
To: [hidden email]
Date: Thu, 12 Jul 2012 02:46:40 -0600

> I'm trying to call a stored procedure by injecting the dataSource into a
> service, and though the call is working, I can't seem to return the result.
>
> As an example, the MySQL sample database called Sakila has a stored
> procedure called film_in_stock, whose signature is:
>
> film_in_stock(IN p_film_id INT, IN p_store_id INT, OUT p_film_count INT)
>
> So I made a service class:
>
> class DataService {
>   def dataSource
>
>   def filmInStock(filmId, storeId) {
>     Sql db = new Sql(dataSource)
>     sql.call('{call film_in_stock(?,?,?)}', [filmId, storeId,
> Sql.INTEGER]) { count ->
>       println count
>       return count
>     }
>   }
> }
>
> I called this from a controller method:
>
> class MyController {
>   def dataService
>
>   def callProc() {
>     render dataService.filmInStock(1,1)
>   }
> }
>
> The result of accessing the controller action callProc is that 4 gets
> printed to the console (as it should), but the procedure returns null,
> which is what is rendered.
>
> Why can I print the value but not return it? Am I missing something obvious?
>
> Thanks,
>
> Ken
> --
> Kenneth A. Kousen
> President
> Kousen IT, Inc.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Can't return a value from a stored procedure

Kenneth Kousen
Yup, that did it. The old "return means return from the closure not the method" trick and I fell for it.

Thanks!

Ken

On Thu, Jul 12, 2012 at 2:54 AM, zyro <[hidden email]> wrote:
i think the return returns only from the closure which is the last
argument for sql.call

sth. like this should work:

def myCount
sql.call(...) { count ->
    myCount = count
}
return myCount

zyro

-------- Original Message  --------
Subject: [grails-user] Can't return a value from a stored procedure
From: Kenneth Kousen <[hidden email]>
To: [hidden email]
Date: Thu, 12 Jul 2012 02:46:40 -0600

> I'm trying to call a stored procedure by injecting the dataSource into a
> service, and though the call is working, I can't seem to return the result.
>
> As an example, the MySQL sample database called Sakila has a stored
> procedure called film_in_stock, whose signature is:
>
> film_in_stock(IN p_film_id INT, IN p_store_id INT, OUT p_film_count INT)
>
> So I made a service class:
>
> class DataService {
>   def dataSource
>
>   def filmInStock(filmId, storeId) {
>     Sql db = new Sql(dataSource)
>     sql.call('{call film_in_stock(?,?,?)}', [filmId, storeId,
> Sql.INTEGER]) { count ->
>       println count
>       return count
>     }
>   }
> }
>
> I called this from a controller method:
>
> class MyController {
>   def dataService
>
>   def callProc() {
>     render dataService.filmInStock(1,1)
>   }
> }
>
> The result of accessing the controller action callProc is that 4 gets
> printed to the console (as it should), but the procedure returns null,
> which is what is rendered.
>
> Why can I print the value but not return it? Am I missing something obvious?
>
> Thanks,
>
> Ken
> --
> Kenneth A. Kousen
> President
> Kousen IT, Inc.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email





--
Kenneth A. Kousen
President
Kousen IT, Inc.

Loading...