Ask Ben: Updating Columns In An Existing ColdFusion Query Object

Posted September 14, 2009 at 8:17 AM

Tags: ColdFusion, Ask Ben

Hi Ben, I have a question:), Is there a way to update a result set that was generated using QuerySetCell. I have a recordset that gets some null values. After the temporary table has been generated, I want to update the empty strings with 0. Is there a way to update query of queries or tables generated by querySetCell method. Thanks.

I'm really glad you asked me this as it got me thinking deeply about ColdFusion query of queries and some of the limitations that they have. What would be really nice (which I experimented with) is if ColdFusion query of queries supported UPDATE and DELETE SQL statements. However, currently, as they do not, the easiest way to solve your problem is to manually loop over the query and explicitly update the appropriate query cell values. To demonstrate this, let's first build a ColdFusion query object from scratch:

 Launch code in new window » Download code as text file »

  • <!--- Create the query object. --->
  • <cfset girls = queryNew( "" ) />
  •  
  • <!--- Add the name column. --->
  • <cfset queryAddColumn(
  • girls,
  • "name",
  • "cf_sql_varchar",
  • listToArray( "Sarah,Libby,Jill,Kim" )
  • ) />
  •  
  • <!---
  • Add the "is cute" column. When we add this column, let'
  • s not supply any default values. This will put NULL
  • value / empty strings in the cells.
  • --->
  • <cfset queryAddColumn(
  • girls,
  • "is_cute",
  • "cf_sql_integer",
  • arrayNew( 1 )
  • ) />
  •  
  •  
  • <!--- Output the query as it stands now. --->
  • <cfdump
  • var="#girls#"
  • label="Incomplete Girls"
  • />

Notice that we are providing default values for the "name" column, but not for the "is_cute" column. As such, the "is_cute" column will be filled with NULL values (represented on the output as empty strings):

 
 
 
 
 
 
ColdFusion Query Objects Can Have NULL Values When Built Manually. 
 
 
 

Now, we have to loop over the query and update the appropriate cells. While we know at this point that all the "is_cute" values will be NULL, I'm going to include a conditional check just so you can see how you might do this if you knew less about the query:

 Launch code in new window » Download code as text file »

  • <!---
  • Now, we want to set the NULL values to zero as the default
  • value. Because the CFQuery query of queries does not support
  • UPDATE statements, we will have to manually loop over the
  • query to find the rows that need to be updated.
  • --->
  • <cfloop query="girls">
  •  
  • <!---
  • Check to see if this "is cute" column is null.
  • NOTE: We know that they all will be at this point, but I
  • wanted to demonstrate the conditional check.
  • --->
  • <cfif !isNumeric( girls.is_cute )>
  •  
  • <!---
  • Since "is cute" is not numeric, we know that it needs
  • to get our default value. When setting this value, it
  • is IMPORTANT that we use JavaCast() so that the
  • unerlying Java object understands what type of value
  • we are setting. Not doing this can lead to query of
  • query errors later on.
  • --->
  • <cfset girls[ "is_cute" ][ girls.currentRow ] = javaCast(
  • "int",
  • 0
  • ) />
  •  
  • </cfif>
  •  
  • </cfloop>
  •  
  •  
  • <!--- Output the query as it stands now. --->
  • <cfdump
  • var="#girls#"
  • label="Defaulted Girls"
  • />

As we loop over the row, we can use the internal index provided implicitly by the CFLoop tag when checking the "is_cute" value; however, when we go to update the row, we need to reference the row index explicitly; as such, updating requires us to use array notation with both the column name and the row index (which we get using the CurrentRow property). As a general rule, you always want to do your best to provide the most accurate Java type when updating a query cell value so as to prevent any odd query of query results later on.

When we run the above code, we get the following output:

 
 
 
 
 
 
To Update A ColdFusion Query Object, You Can Loop Over It And Set The Cell Values Explicitly. 
 
 
 

As you can see, the "is_cute" column now has its default zeros. I hope that helps.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Oct 5, 2009 at 2:53 AM // reply »
4 Comments

Hi,
It's good question and your answer is also excellent.The code works great for me for my project.Thanks for this article.


Oct 9, 2009 at 12:12 PM // reply »
2 Comments

Thanks Ben. As usual, a very useful post!


Post Comment  |  Ask Ben

Recent Blog Comments
Feb 9, 2010 at 5:34 AM
Creating A "Remember Me" Login System In ColdFusion
Any change you could show how to take advantage how the J2ee session stuff in your code? http://kb2.adobe.com/cps/182/tn_18232.html ... read »
Feb 9, 2010 at 5:32 AM
Creating A "Remember Me" Login System In ColdFusion
This may help: http://bugs.farcrycms.org/browse/FC-79 Its the session variables' option being selected in the CF Admin ... read »
Feb 9, 2010 at 4:14 AM
Ask Ben: Converting a Query to an Array
Exellent script! Tried to make it shorter, gained approx 10% performance improvement :) [CODE] <cffunction name="queryToArray" access="public" returntype="array" output="false"hint="This turns a q ... read »
Feb 9, 2010 at 3:16 AM
Using jQuery's SlideUp() and SlideDown() Methods With Bottom-Positioned Elements
Just a quick fix for the code example above: $('.panel').height() should instead be... $('.panel').outerHeight() ... read »
Feb 9, 2010 at 3:09 AM
Using jQuery's SlideUp() and SlideDown() Methods With Bottom-Positioned Elements
I came across this page because I was searching for a certain behavior. I didn't just want the element to slide up; I wanted it's contents to slide up with it. As if the contents were written on a p ... read »
Feb 9, 2010 at 12:57 AM
Ask Ben: Creating A PDF And Attaching It To An Email Using ColdFusion
Have you got any reference to code explaining the procedure to use cfdocument, file to disk and attach to email? ... read »
Feb 8, 2010 at 11:27 PM
Why NULL Values Should Not Be Used in a Database Unless Required
@Randi, While I can appreciate your specific situation, I personally am not a fan of a situation where people have the option to run their own ad-hoc reports on the database. This is, honestly, a r ... read »
Feb 8, 2010 at 11:15 PM
Creating Microsoft Excel Documents With ColdFusion And XML
@Candice, No problem - glad you got it figured out. @David, To view the XLS document as an XML file, if I remember correctly, I actually opened up the document in Excel and then went "File > ... read »