When I am in a script block, how can I test for the existence of a column?
Testing for existence of column in a query (I assume you mean a query column) is easy inside of a CFScript block in ColdFusion. Actually, it's easy anywhere: all you have to do is check to see if it is in the ColdFusion query's ColumnList attribute:
Launch code in new window » Download code as text file »
This uses searches through the column list of the query. I am never sure what case this list comes back in (upper, lower, mixed) and so I do a NoCase find method call. This should return the index of the matching item. If there is no matching item, then it returns zero which ColdFusion will interpret as False.
You can also reference a ColdFusion query object like it was a struct with keys. These keys, among other things, contain the columns. Therefore, you can also check for key existence:
Launch code in new window » Download code as text file »
Now, you asked about doing this inside of ColdFusion CFScript tags. These are just simple list and key checking operations and can be easily translated to CFScript:
Launch code in new window » Download code as text file »
You would of course replace qQueryObject with name of your query and column_name with the name of your query. Good luck!
Download Code Snippet ZIP File
Comments (0) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Ask Ben: Getting A Random Date From A Date Range In SQL
Ask Ben: Finding The SQL Data Type Of A ColdFusion Query Column
There are no comments posted for this web log entry.