Skip to content

Instantly share code, notes, and snippets.

@JamoCA
Last active January 6, 2017 00:13
Show Gist options
  • Save JamoCA/5a2adb52cbeb4e15337a7d899222072e to your computer and use it in GitHub Desktop.
Save JamoCA/5a2adb52cbeb4e15337a7d899222072e to your computer and use it in GitHub Desktop.
Small update to QueryAppend to prevent NULL values (dates & numbers) from being incorrectly recast to an invalid "empty string" by ColdFusion's Query-of-Queries.
<!--- 7/5/2006 QueryAppend By Ben Nadel https://www.bennadel.com/blog/114-coldfusion-queryappend-qone-qtwo.htm
1/5/2017 New "EmptyAsNull" option to prevent NULL values (dates & numbers) from being incorrectly recast
to an invalid "empty string" by ColdFusion's Query-of-Queries and throwing "Error casting an object
of type to an incompatible type" error. --->
<cffunction name="QueryAppend" access="public" returntype="void" output="false" hint="This takes two queries and appends the second one to the first one. This actually updates the first query and does not return anything.">
<cfargument name="QueryOne" type="query" required="true">
<cfargument name="QueryTwo" type="query" required="true">
<cfargument name="EmptyAsNull" default="" required="false">
<cfset var LOCAL = StructNew()>
<cfset LOCAL.Columns = ListToArray(ARGUMENTS.QueryOne.ColumnList)>
<cfset LOCAL.EmptyAsNull = 0>
<cfif isValid("boolean", ARGUMENTS.EmptyAsNull) AND ARGUMENTS.EmptyAsNull>
<cfset LOCAL.EmptyAsNull = 1>
</cfif>
<cfloop query="ARGUMENTS.QueryTwo">
<cfset QueryAddRow(ARGUMENTS.QueryOne)>
<cfloop ARRAY="#LOCAL.Columns#" index="LOCAL.ColumnName">
<cfif StructKeyExists(ARGUMENTS.QueryTwo, LOCAL.ColumnName) AND (NOT LOCAL.EmptyAsNull OR LEN(ARGUMENTS.QueryTwo[LOCAL.ColumnName][ARGUMENTS.QueryTwo.CurrentRow]))>
<cfset ARGUMENTS.QueryOne[LOCAL.ColumnName][ARGUMENTS.QueryOne.RecordCount] = ARGUMENTS.QueryTwo[LOCAL.ColumnName][ARGUMENTS.QueryTwo.CurrentRow]>
</cfif>
</cfloop>
</cfloop>
<cfreturn>
</cffunction>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment