Ask Ben: Updating Columns In An Existing ColdFusion Query Object

Posted September 14, 2009 at 8:17 AM by Ben Nadel

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:

  • <!--- 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:

  • <!---
  • 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.




Reader Comments

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

Thanks Ben. As usual, a very useful post!


Apr 1, 2010 at 5:47 PM // reply »
9 Comments

thanx,
very useful info


Jan 8, 2013 at 8:36 AM // reply »
2 Comments

Hi Ben,

I have also an Query, which I want to convert to a Json-Object.

Within my Query, there is a String(for example "0000" or "0001"). Unfortunately, this String is after the SeriealizeJson a number in the Json(0,1 ...).

I get an error when calling the functions with jQuery because the Json-Object isnt valid. The quotation marks are fail:

{
"COLUMNS": [
"Test1",
"Test2",
"Test3"
],
"DATA": [
[
"AA ",
0000,
"collecteur d'échappement "
]
]
}

I already tried so many things ... Can you help me?

Here my Code:

<cffunction name="getData" access="remote" returntype="any" returnformat="plain">

SQL....

<cfset result = SerializeJSON(result)>

<cfreturn result>
</cffunction>

Thanks a lot Ben.


Feb 13, 2013 at 10:42 AM // reply »
3 Comments

Morpheus - I was having a similar problem with CF JSON truncating strings with leading zeros and here's how I handled it. I know the string length should be 6 - so it could be 000000, or A12345 etc.

  • <cfif StructKeyExists(LOCAL.returnQry,"group_id")>
  • <cfif LEN(LOCAL.returnQry.group_id) LT 6>
  • <cfset LOCAL.returnQry["group_id"][LOCAL.returnQry.CurrentRow] = NumberFormat(LOCAL.returnQry.group_id,'000000')/>
  • </cfif>
  • </cfif>

If my group_id begins with a letter it's fine, if not I know it's 6 digits numeric and I just need to preserve the leading zeros.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 19, 2013 at 2:31 PM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
It's funny really just how well that image describes the way I would imagine most people that go with angular for some project is. I have had a similar roller-coaster ride with it as well, but not qu ... read »
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 10:37 PM
Very Simple Pusher And ColdFusion Powered Chat
hi id making plz easy ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
May 15, 2013 at 4:15 PM
What If All User Interface (UI) Data Came In Reports?
@Josh, Thanks! @Ben, I definitely recommend the David West book "Object Thinking" I've been quoting from. It goes deeply into the philosophy and history of OO programming. His breadth ... read »
May 15, 2013 at 11:36 AM
Ask Ben: Print Part Of A Web Page With jQuery
I found this helpfull when you need to keep (refresh) the original parent page after closing the iframe child print dialog (Hoping you're not using a form at this time so it won't submit again): On ... read »
May 14, 2013 at 7:13 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, If there's any books you'd recommend on the subject of domain modelling, I'd love to hear it. I just downloaded the free PDF of "Domain Driven Design Quickly". Figured I'd give it ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools