mawburn
mawburn

Reputation: 2335

How can I retrieve a SQL variable from cfquery?

I am running this inside of my cfquery.

SET @rID = ( SELECT TOP 1 roleid
             FROM Roles
             WHERE RoleName = @rName AND appid = @appID
             ORDER BY Created DESC);

Is it possible to retrieve @rID without having to run the SELECT query a second time? As in:

<cfset varName = queryName.rID>

The above doesn't work obviously, but is there any other way to return the variable from the query?

Upvotes: 4

Views: 223

Answers (1)

nosilleg
nosilleg

Reputation: 2153

You can get the value of @rID by selecting it without the need to run the full query again.

<cfquery name="qryRoleID">
    SET @rID = ( SELECT TOP 1 roleid
         FROM Roles
         WHERE RoleName = @rName AND appid = @appID
         ORDER BY Created DESC);
    SELECT @rID AS rID
</cfquery>
<cfdump var="#qryRoleID.rID#">

Upvotes: 9

Related Questions