Reputation: 23868
In my cfoutput
tag, I've set a variable that stores the name of a customer returned from a query. Then I use that variable in the WHERE
clause in cfquery
tag to retrieve the customer info. But I'm getting the error Variable CUSTOMER is undefined
under certain search criteria when the form is submitted. I think the error occurs when CUSTOMER variable is null. I've tried various options in the following Where clause such as where Customers.CustomerName = #IIF(isDefined('Form.CUSTOMER'),de(Form.CUSTOMER),de(''))#
but I still get the same error. I'm using SQL Server 2012
on the backend. Note that I cannot use cfparam
inside cfoutput
:
<cfoutput query="qry_customers">
<cfset CUSTOMER = #CUSTOMER#>
</cfoutput>
<cfquery name="get_customers" datasource="#request.dsn_name#">
SELECT * from Customers
where Customers.CustomerName = '#CUSTOMER#'
</cfquery>
UPDATE The error occurs in the WHERE
clause in the cfquery
tag below. And it occurs only when
select statement in the cfquery tag does not return any records. The CF error shows the line # of the error to be the line where the WHERE clause is.
Upvotes: 0
Views: 3472
Reputation: 3558
I'd recommend trying a few things:
<cfset CUSTOMER = #CUSTOMER#>
is necessary. If I read it correctly, you're just setting a variable name as the exact same name. Also, it appears that this is a text field (based on your query that matches it to a column named "CustomerName" rather than something like "CustomerNumber"). So, at a minimum, you'd need to include the quotation marks: <cfset CUSTOMER = "#CUSTOMER#">
.qry_customers.CUSTOMER
instead of just CUSTOMER
.<cfqueryparam>
tag (e.g., <cfqueryparam value="#qry_customers.CUSTOMER#", CFSQLType="CF_SQL_VARCHAR">
)value="#trim(qry_customers.CUSTOMER & ' ')#"
.Upvotes: 2
Reputation: 1542
You'll get that error anytime qry_customers has no rows. Just reference qry_customer.customer directly. (If there were no rows, this will be equivalent to an empty string. You may end up wanting to check qry_customers.RecordCount instead.)
Upvotes: 2