Skip navigation
Currently Being Moderated

possiblilities for null=  in <cfqueryparam

Jan 25, 2012 7:48 AM

why can't I do this:

 

<cfqueryparam value="#structFromParsedXML[d].ClientID#" cfsqltype="cf_sql_varchar" null="not structKeyExists(structFromParsedXML[d], 'ClientID')"

 

whats the best workaround for this?

 
Replies
  • Currently Being Moderated
    Jan 25, 2012 7:59 AM   in reply to nikos101

    You can do that. What's the problem?

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 25, 2012 8:06 AM   in reply to nikos101

    why can't I do this:

     

    <cfqueryparam value="#structFromParsedXML[d].ClientID#" cfsqltype="cf_sql_varchar" null="not structKeyExists(structFromParsedXML[d], 'ClientID')"

     

     

     

    Two reasons:

    1) the expression you have in your NULL attribute is a string.  It needs to be boolean.

    2) all the attribute values for a tag need to be defined.  So if your null condition was true, your VALUE value would be invalid.

     

    You need to wrap your <cfqueryparam> tag in an if/else block which checks the key exists, and either use it or send just a null.

     

    --

    Adam

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 25, 2012 8:25 AM   in reply to nikos101

    While the parameter must exists, you can do logic like this (I use it all the time):

     

    <cfparam name="structFromParsedXML[d].ClientID" default="" />

    <cfquery...>

      ...

      clientID = <cfqueryparam value="#structFromParsedXML[d].ClientID#" cfsqltype="cf_sql_varchar" null="#evaluate('NOT len(structFromParsedXML[d].ClientID)')#" />

    </cfquery>

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 25, 2012 9:01 AM   in reply to Steve Sommers

    Just a quick point that you should not need to use the evalute function in this case.

     

    <cfqueryparam value="#structFromParsedXML[d].ClientID#" cfsqltype="cf_sql_varchar" null="#NOT len(structFromParsedXML[d].ClientID)#" />

     

    Should work just fine.

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 26, 2012 2:54 AM   in reply to nikos101

    nikos101 wrote:

     

    why can't I do this:

     

    <cfqueryparam value="#structFromParsedXML[d].ClientID#" cfsqltype="cf_sql_varchar" null="not structKeyExists(structFromParsedXML[d], 'ClientID')"

     

    whats the best workaround for this?

    Actually, the logic you wish to apply is a bit subtle. If the key ClientID does not exist in the struct, then you want the value to be NULL. ColdFusion would then ignore the value attribute, whatever it is.

     

    However, I do believe you could improve the code logic by doing something like

     

    Outside cfquery tag:

     

    <cfset isNullValue = true>

    <cfset id          = "">

     

    <cfif structKeyExists(structFromParsedXML[d], 'ClientID')>

        <cfset isNullValue = false>

        <cfset id     = structFromParsedXML[d].ClientID>

    </cfif>

     

     

    Within cfquery tag:

     

    <cfqueryparam value="#id#" cfsqltype="cf_sql_varchar" null="#isNullValue#">

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points