{"id":16,"date":"2015-09-16T16:08:01","date_gmt":"2015-09-16T16:08:01","guid":{"rendered":"http:\/\/www.blueradial.com\/blog\/?p=16"},"modified":"2015-09-24T19:42:29","modified_gmt":"2015-09-24T19:42:29","slug":"query-of-queries","status":"publish","type":"post","link":"http:\/\/www.blueradial.com\/blog\/index.php\/2015\/09\/16\/query-of-queries\/","title":{"rendered":"Query of Queries Tricks"},"content":{"rendered":"<p>ColdFusion&#8217;s Query of Queries is a fantastic tool. It isn&#8217;t nearly as fast as running a query on a database directly, but it serves a very useful purpose. More often than I&#8217;d care to remember, there&#8217;s a need for combining a query set from multiple datasources that have no way to talk to each other. There&#8217;s ways to get around this, of course, but QoQ makes things just so much simpler.<\/p>\n<p>Except, when it isn&#8217;t so simple.<\/p>\n<p>(Sigh)<\/p>\n<p>Anyways, I had a project in which I needed to use QoQ, and it happened to be within a CFC function. I try to always use locally scoped variables when possible, so, I ran into a silly problem: How to use QoQ with a local scoped query. Because, I can&#8217;t just use &#8220;local.queryName&#8221; in the query name &#8211; it throws an error.<\/p>\n<p>Second, this QoQ needed to combine datasets that did not exactly match up. For example, 2 of the 3 queries that I was combining did not have a &#8220;description&#8221; field. QoQ acts funny sometimes when you try to hard-code a value &#8211; it seems to guess what the datatype should be, and oftentimes gets it wrong. You&#8217;ll then get an error about a datatype mismatch. So, I needed to be able to force casting of variable types.<\/p>\n<p>For problem #1, <a href=\"http:\/\/www.bennadel.com\/blog\/94-local-variables-scope-conflicts-with-coldfusion-query-of-queries.htm\" target=\"_blank\">Ben Nadel&#8217;s blog has a perfect solution<\/a>. Use [local].queryName instead of local.queryName.<\/p>\n<p>For the second issue, <a href=\"http:\/\/www.bennadel.com\/blog\/379-coldfusion-query-of-queries-unexpected-data-type-conversion.htm\" target=\"_blank\">Ben Nadel to the rescue again<\/a>!<\/p>\n<div><em>(These are both very old posts, but, they are still relevant and useful&#8230;)<\/em><\/div>\n<hr \/>\n<h3>Final code:<\/h3>\n<pre class=\"brush: coldfusion; title: ; notranslate\" title=\"\">\r\n\t&lt;cffunction name=&quot;getCalendarData&quot; access=&quot;public&quot; returntype=&quot;any&quot; hint=&quot;&quot; output=&quot;false&quot;&gt; \r\n\t\t&lt;cfargument name=&quot;startDay&quot; type=&quot;date&quot; required=&quot;no&quot; default=&quot;#CreateDate(Year(Now()),Month(Now()),1)#&quot;&gt;\r\n\t\t&lt;cfargument name=&quot;endDay&quot; type=&quot;date&quot; required=&quot;no&quot; default=&quot;#CreateDate(Year(Now()),Month(Now()),DaysInMonth(Now()))#&quot;&gt;\r\n\t\t\r\n\t\t&lt;cfset var local={}&gt;\r\n\t\t\r\n\t\t&lt;!--- get CALENDAR dates ---&gt;\r\n\t\t&lt;cfset local.calendarData    = getEventsForDateRange(arguments.startDay,arguments.endDay)&gt;\r\n\t\t&lt;!--- get ANNOUCEMENT dates ---&gt;\r\n\t\t&lt;cfset local.annoucementData = getCalendarAnnoucementsForDateRange(arguments.startDay,arguments.endDay)&gt;\r\n\t\t&lt;!--- get Meetings dates ---&gt;\r\n\t\t&lt;cfset local.meetingsData    = getMeetingsForDateRange(arguments.startDay,arguments.endDay)&gt;\r\n\t\t\r\n\t\t&lt;!--- and now, we merge! ---&gt;\r\n\t\t&lt;cfquery dbtype=&quot;query&quot; name=&quot;local.allCalendarData&quot;&gt;\r\n\t\t\tselect CAST(title as VARCHAR), \r\n\t\t\t       start_date, end_date, \r\n\t\t\t       CAST(link as VARCHAR), \r\n\t\t\t       CAST(description as VARCHAR), 'C' as data_type\r\n\t\t\tfrom [local].calendarData\r\n\t\t\tUNION\r\n\t\t\tselect CAST(page_title as VARCHAR) as title, \r\n\t\t\t       page_announcement_start_date as start_date,\r\n\t\t\t       page_announcement_end_date as end_date, \r\n\t\t\t       CAST(announcement_url as VARCHAR) as link, \r\n\t\t\t       CAST('' as VARCHAR) as description, 'A' as data_type\r\n\t\t\tfrom [local].annoucementData\r\n\t\t\tUNION\r\n\t\t\tselect CAST(event_title as VARCHAR) as title, \r\n\t\t\t       event_start_date as start_date, event_end_date as end_date, \r\n\t\t\t       CAST(event_url as VARCHAR) as link, \r\n\t\t\t       CAST('' as VARCHAR) as description, 'M' as data_type\r\n\t\t\tfrom [local].meetingsData\r\n\t\t\torder by start_date, title\r\n\t\t&lt;\/cfquery&gt;\r\n\r\n\t\t&lt;cfreturn local.allCalendarData&gt;\r\n\t&lt;\/cffunction&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>ColdFusion&#8217;s Query of Queries is a fantastic tool. It isn&#8217;t nearly as fast as running a query on a database directly, but it serves a very useful purpose. More often than I&#8217;d care to remember, there&#8217;s a need for combining a query set from multiple datasources that have no way to talk to each other. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[9,8],"_links":{"self":[{"href":"http:\/\/www.blueradial.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/16"}],"collection":[{"href":"http:\/\/www.blueradial.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.blueradial.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.blueradial.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.blueradial.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=16"}],"version-history":[{"count":3,"href":"http:\/\/www.blueradial.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/16\/revisions"}],"predecessor-version":[{"id":43,"href":"http:\/\/www.blueradial.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/16\/revisions\/43"}],"wp:attachment":[{"href":"http:\/\/www.blueradial.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.blueradial.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.blueradial.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}