If I’m a running a database query/stored procedure in Coldfusion, what is the proper way to reference fields returned from the query?
<cfstoredproc procedure="proc_select_extern" datasource="stokkers">
<cfprocparam type="in" value="#Session.Extern#" cfsqltype="cf_sql_varchar" maxlength="13">
<cfprocresult name="extern">
</cfstoredproc>
<cfoutput query="extern">
<cfset variables.some = extern.foo>
OR
<cfset variables.some = foo>
</cfouput>
Say extern includes foo, bar and foobar. Is it allowed and better to write:
extern.foo;
extern.bar;
extern.foobar;
because I’m running through a page and often find these “naked” variables a little confusing to follow:
foo;
bar;
foobar;
There is a lot of info on scopes and proper scoping but I have not found anything on query-output.
Thanks for clarification!
Some will tell you that it is good habitual practice to always scope because it keeps you from making scoping errors where it is really important.
Personally in views I like the approach of using cfoutput with a query and NOT having to scope – it’s the equivalent of “WITH” in other languages. Since the query will always be evaluated before form and url scopes within a query driven cfoutput tag I do not see any issues with leaving off the scope in that instance. Keep in mind that in CFCs the “arguments” and local scope will both be preemptive – but that’s not the best place for a query driven cfoutput – which is designed (ably designed) for convenient display.
But again.. others will tell you different (with some passion as well 🙂 .