Skip to main content
Participant
March 3, 2011
Question

Capture return value from a SQL statement

  • March 3, 2011
  • 2 replies
  • 665 views

Hello all,

I would like to capture and display the return value of an SQL statement. Below given is the code:

<cfcomponent>   
    <cffunction name="addship" access="remote">
        <cfquery name="shippers" datasource="northwindodbc" result="maxshipperid">
            select max(shipperid) from shippers
        </cfquery>                       
    </cffunction>
</cfcomponent>

The above given SQL statement is returning the max shipperid which in this example has to be 3. How do I capture this return value on an external variable which can be accessed outside the cfquery statement.

Any quick response is highly appreciated.

Anil

    This topic has been closed for replies.

    2 replies

    Inspiring
    March 3, 2011

    You need to include the "AS xxx" in the query so that you have a name to reference it as outside of the query:

    <cfquery name="shippers" datasource="northwindodbc" result="maxshipperid">
                select max(shipperid) AS maxShpperID from shippers
            </cfquery>

    Max Id is #shippers.maxShipperID#

    -reed

    Participant
    March 3, 2011

    I just cracked it before looking at your reply.. But thanks very much for the quick response. Appreciate it.

    Anil

    12Robots
    Participating Frequently
    March 3, 2011

    The result of your query is in the variable called "shipper".  It is the same as the name of your query. You can also return that from your function.

    <cfreturn shipper />