Skip to main content
Participating Frequently
December 3, 2008
Answered

Assigning a Variable a Null Value

  • December 3, 2008
  • 2 replies
  • 687 views
Hi all. I've got a loop that queries a query outside of the loop. The problem is, the query may get defined, and it may not. But if it gets defined, how do I undefine it for the next loop iteration. Here's some code to wrap your head around it (edited for brevity).

<cfloop>
<cfset queryValue = possibleEmptyString >
<cfif Len(queryValue)>
<cfquery name="queryName" dbtype="query">
select * from something
</cfquery>
</cfif>

<cfif IsDefined("queryName") AND queryName.RecordCount>
error="some error message"
</cfif>
</cfloop>

So, is there not a Function that sets the var to Undefined, or Null, or something? If not, what's the alternative?

Thanks all! 😉
    This topic has been closed for replies.
    Correct answer Kronin555
    <cfloop>
    <cfset queryValue = possibleEmptyString >
    <cfset queryName = QueryNew()> <!--- this ensures that queryName is always defined, and is always a query --->
    <cfif Len(queryValue)>
    <cfquery name="queryName" dbtype="query">
    select * from something
    </cfquery>
    </cfif>

    <cfif queryName.RecordCount>
    error="some error message"
    </cfif>
    </cfloop>

    2 replies

    Participating Frequently
    December 4, 2008
    Thanks Dan. But, that won't quite solve my problem.

    The problem is that once it becomes a query, I can't reset it back to a String value because I've already turned it into a complex type so IsQuery() it will always return true.

    not sure how using a try/catch will solve it either. Am I missing something here?

    Thanks for your reply, I appreciate the attentiont to this issue.
    Kronin555Correct answer
    Participating Frequently
    December 4, 2008
    <cfloop>
    <cfset queryValue = possibleEmptyString >
    <cfset queryName = QueryNew()> <!--- this ensures that queryName is always defined, and is always a query --->
    <cfif Len(queryValue)>
    <cfquery name="queryName" dbtype="query">
    select * from something
    </cfquery>
    </cfif>

    <cfif queryName.RecordCount>
    error="some error message"
    </cfif>
    </cfloop>
    Inspiring
    December 3, 2008
    You can set a var to an empty string. For what you appear to be attempting, either cftry/cfcatch, or isQuery() might be useful.