Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 687
  • Last Modified:

Results of a query in cfscript in the next query.

I'm building a report output page that needs to pull records (UserID) from from the table that associates a user with a group (groupAssociation) and then use each of those IDs to pull the records out of a data table.  For arguments sake it's a list of the userid and a value. My included script is the start of this but CF is showing an error and saying that "Variable CFQUERY is undefined."  

<cfscript>
  sqlstring = "select UserID from groupAssociation where groupid="&form.groupid;
  datasource = request.dsn;
  username = request.dbusername;
  password = request.dbpassword;
  maxrows = attributes.maxrows;
  getGroupMembers = CFQUERY(sqlstring: sqlstring, datasource: datasource, username: username, password:password, maxrows: maxrows);

    // initialize Java StingBuffer Object
    SQL = createObject("java", "java.lang.StringBuffer");
      SQL.append("select ID, Name, groupParticipate from groupName where ");
    // build the SQL statement
    for (i = 1; i lt getGroupMembers.length(); i = i + 1){
           SQL.append("ID = " & getGroupMembers.UserID & " and ");
    }
      // the last one does not need an and.
      SQL.append("ID = " & getGroupMembers.UserID);
    // display string
    getGroupResults = CFQUERY(sql:string: variables.SQL.toString(), , datasource: datasource, username: username, password:password);

</cfscript>

results: <cfoutput query=getGroupResults> #userid#<br />#value#

Help me see what I am doing wrong
0
leeandron
Asked:
leeandron
  • 2
1 Solution
 
MauseCommented:
Hi there

cfquery is not available in cfscript!

In:
CFQUERY(sqlstring: sqlstring, datasource: datasource, username: username, password:password, maxrows: maxrows);

CF wants to call a function cfquery wicth is also not defined or is it?

Mause
0
 
smaglio81Commented:
Maybe try some of these minor changes (If you are using CFMX6.1) ...

<cfscript>
  datasource = request.dsn;
  username = request.dbusername;
  password = request.dbpassword;
  maxrows = attributes.maxrows;
</cfscript>

<cfquery name="getGroupMembers" datasource="#datasource#" username="#username#" password="#password#" maxrows="#maxrows#">
    SELECT   UserID
    FROM     groupAssociation
    WHERE   groupid = <cfqueryparam cfsqltype="cf_sql_integer" value="#form.groupid#"
                                       null="#YesNoFormat( not len(form.groupid) )#" />
</cfquery>

<cfscript>

    // initialize the next SQL statement string
    SQL = "select ID, Name, groupParticipate from groupName where ";

    // build the SQL statement
    for (i = 1; i lt getGroupMembers.length(); i = i + 1){
           SQL = SQL & "ID = " & getGroupMembers.UserID[i] & " and ";
    }

     // the last one does not need an and.
     SQL = SQL & "ID = " & getGroupMembers.UserID[i];

</cfscript>

<cfquery name="getGroupResults" datasource="#datasource#" username="#username#" password="#password#">
    <cfoutput>#SQL#</cfoutput>
</cfquery>

results:
<cfoutput query="getGroupResults">
    #userid#<br />
    #value#<br />
</cfoutput>


These changes are mostly ascetic, but they might clear up that "variable CFQUERY is undefined" error.

When coldfusion spits out an error in the form of "variable xxxxx is undefined", and in your code xxxxx is a method call; that means that the method xxxxx() is not a known method; at which point CF assumes it is a variable. When CF cant find that variable then it hickups and spits out the above error message.

I am pretty sure that you are correct in assuming that there is a way to execute queries within <cfscript> tags; but at the moment I am at a loss to what it is. I know that the O'Reilly book (http://www.oreilly.com/catalog/coldfusion2/) describes how to do it; but that book is sitting on a shelve at my work. Another good reference is the CFML reference at Macromedia.com (http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/part_cfm.htm).

The other thing that I noticed is during your loop you were getting the proper field, but it didn't look like you had referenced what record you wanted that field from. That's why I added the "[i]". You might be able to get the appropriate information the way you had it set up, but I have never checked.

I hope that helps.

Steven
0
 
leeandronAuthor Commented:
Mause, I had gotten some of the ideas for what I was trying to do from this article:
http://www.macromedia.com/devnet/mx/coldfusion/extreme/cftags_cfscript.html
it explained (but I had misunderstood before I sent this question) how to set up a cfcomponent that you could call from within your cfscript.  Once that is instanciated you can make a query without leaving the cfscript:

<cfcomponent>
      <cffunction name="QUERY" access="public" returntype="query">
            <cfargument name="SQLString" type="string" required="yes">
            <cfargument name="Datasource" type="string" required="no" default="#request.dsn#">
            <cfargument name="Username" type="string" required="no" default="#request.dbusername#">
            <cfargument name="password" type="string" required="no" default="#request.dbpassword#">
            <cfargument name="maxrows" type="string" required="no" default="1000">
            <CFQUERY NAME="RecordSet" Datasource="#arguments.Datasource#" username="#arguments.Username#" password="#arguments.password#" maxrows="#arguments.maxrows#">
                  #preserveSingleQuotes(arguments.SQLString)#
            </CFQUERY>
            <cfreturn RecordSet>
      </cffunction>
</cfcomponent>

However that requires another file in the root and adds some complexity.  I'm going to try out steven's answer and I'll be back.
0
 
MauseCommented:
If you have a function cfquery you could call it within cfscript
Now you've got a component just call componentname.query to call the function query.

if you just past this in the same source it will work:

<cffunction name="CFQUERY" access="public" returntype="query">
  <cfargument name="SQLString" type="string" required="yes">
  <cfargument name="Datasource" type="string" required="yes">
  <cfargument name="dbType" type="string" default="">            
  <cfquery name="RecordSet" datasource="#arguments.Datasource#"
    dbtype="#arguments.dbType#">
      #preserveSingleQuotes(arguments.SQLString)#
 </cfquery>
 <cfreturn RecordSet>
</cffunction>

0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now