Dynamic variable names in ColdFusion

  • Post author:
  • Post category:Uncategorized

This is probably old news for most of the advanced CF developers out there, but I figured out how to do dynamic variable names today. The situation was this: I need to do an arbitrary amount of SQL queries, in other words, I won’t know ahead of time how many queries I have perform. But I need to access the data from these queries, so they need to have unique names. Here is my code:select amount from finalQuerywhere acct = #acctsArray[i]#and db2date <= '#lastFromDate.date2#'#acctsArray[i]#: #evaluate("dataByAcct#acctsArray[i]#.amount")#
So I had a list of accounts, and I didn’t know how many accounts would be there each time this code fires. You can see that I am looping over the accounts list, and doing a query for each account. But the magic is that the name of my query will change each time to match the current account. Finally, I had to use the evaluate function to get the data using my dynamic query name.