• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

possiblilities for null= in <cfqueryparam

Enthusiast ,
Jan 25, 2012 Jan 25, 2012

Copy link to clipboard

Copied

why can't I do this:

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

whats the best workaround for this?

TOPICS
Advanced techniques

Views

1.9K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 26, 2012 Jan 26, 2012

nikos101 wrote:

why can't I do this:

<cfqueryparam value="#structFromParsedXML.ClientID#" cfsqltype="cf_sql_varchar" null="not structKeyExists(structFromParsedXML, '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

Outsid

...

Votes

Translate

Translate
Guide ,
Jan 25, 2012 Jan 25, 2012

Copy link to clipboard

Copied

You can do that. What's the problem?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 25, 2012 Jan 25, 2012

Copy link to clipboard

Copied

why can't I do this:

<cfqueryparam value="#structFromParsedXML.ClientID#" cfsqltype="cf_sql_varchar" null="not structKeyExists(structFromParsedXML, '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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jan 25, 2012 Jan 25, 2012

Copy link to clipboard

Copied

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

<cfparam name="structFromParsedXML.ClientID" default="" />

<cfquery...>

  ...

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

</cfquery>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jan 25, 2012 Jan 25, 2012

Copy link to clipboard

Copied

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

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

Should work just fine.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 26, 2012 Jan 26, 2012

Copy link to clipboard

Copied

nikos101 wrote:

why can't I do this:

<cfqueryparam value="#structFromParsedXML.ClientID#" cfsqltype="cf_sql_varchar" null="not structKeyExists(structFromParsedXML, '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, 'ClientID')>

    <cfset isNullValue = false>

    <cfset id     = structFromParsedXML.ClientID>

</cfif>

Within cfquery tag:

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 27, 2012 Jan 27, 2012

Copy link to clipboard

Copied

LATEST

thanks guys, love the answers, I'll implement bkbk's

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation