Solved

Results of a query in cfscript in the next query.

Posted on 2004-09-26
4
648 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

A web service (http://en.wikipedia.org/wiki/Web_service) is a software related technology that facilitates machine-to-machine interaction over a network. This article helps beginners in creating and consuming a web service using the ColdFusion Ma…
Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

707 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