user3239126
user3239126

Reputation:

SQL Query breaking for some reason

Please consider the following Image:

enter image description here

When I ran the following query in coldfusion:

<cfquery datasource="mydb" name="qCoulmnInsert">
INSERT INTO 
simplexresults.contactresults_email_account_summary_devices 
(open_desktop_int,
open_other_int,
open_phone_int,
open_tablet_int,
open_webmail_int,  
<!--- FOR Unique Open --->
unique_webMail_int,
unique_tablet_int,
unique_other_int,
unique_phone_int,
unique_desktop_int,
<!--- FOR DATES --->
startdate_dt,
enddate_dt,
date_dt)

VALUES
<!--- loop through your array --->
<cfloop from="1" to="#arrayLen(cfData)#" index="i">
(
<cfif structKeyExists(cfData[i], "open")>
<cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i].open.Desktop#">
<cfelse>
NULL
</cfif> ,
<cfif structKeyExists(cfData[i], "open")>
<cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i].open.Other#">
<cfelse>
NULL
</cfif> ,


<cfif structKeyExists(cfData[i], "open")>
<cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i].open.Phone#">
<cfelse>
NULL
</cfif> ,

  and so on and so forth ...
                   ....

</cfquery>

I get the following error:

 Element OPEN.DESKTOP is undefined in a CFML structure referenced as part of an expression.

The error occurred in C:\myfile.cfm: line 55

53 :              (
54 :               <cfif structKeyExists(cfData[i], "open")>
55 :                 <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i].open.Desktop#">
56 :               <cfelse>
57 :                 NULL

I know it's not defined for some of the structures as shown in the image below, but I have included "NULL" in my <cfelse> statement to handle those cases. Am I doing something wrong here? Please advise.

However the query runs fine for the date range where OPEN'S are defined.

Correct Way:

<cfif structKeyExists(cfData[i], "open")>
                <cfif  structKeyExists(cfData[i].open,"Desktop")>
                <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Desktop"]#">
                <cfelse>
                 NULL
                </cfif>
              <cfelse>
                NULL

              </cfif> ,

Upvotes: 1

Views: 94

Answers (1)

Chris Tierney
Chris Tierney

Reputation: 1549

You qualify that "open" is available in your if statement, but "Desktop" is not always available and is not wrapped in a qualifying statement. If other words, "Desktop" is not always a key, but your loop always looks for it as long as the "open" key is defined.

Upvotes: 7

Related Questions