Solved

Results of a query in cfscript in the next query.

Posted on 2004-09-26
4
638 Views
Last Modified: 2013-12-24
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
Comment
Question by:leeandron
  • 2
4 Comments
 
LVL 10

Expert Comment

by:Mause
ID: 12155757
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
 
LVL 5

Accepted Solution

by:
smaglio81 earned 500 total points
ID: 12155787
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
 

Author Comment

by:leeandron
ID: 12155904
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
 
LVL 10

Expert Comment

by:Mause
ID: 12155914
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

Superior storage. Superior surveillance.

WD Purple drives are built for 24/7, always-on, high-definition security systems. With support for up to 8 hard drives and 32 cameras, WD Purple drives are optimized for surveillance.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now