Solved

Results of a query in cfscript in the next query.

Posted on 2004-09-26
4
637 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 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

23 Experts available now in Live!

Get 1:1 Help Now