Is name attribute of cfquery optional? The doc said it is required, but my code seems to run fine even without it. Is there a default value if not defined? and if so, shall I local var it or safely ignore it?
Thx
Is name attribute of cfquery optional? The doc said it is required, but my code seems to run fine even without it. Is there a default value if not defined? and if so, shall I local var it or safely ignore it?
Thx
I think you can safely omit the name
. It does not appear to add a default name
to the variables or local scopes.
However, I did notice something strange under CF9. If you do not local scope the result
, the key cfquery.executiontime
is added to the variables
scope. This is with all debugging disabled (unless I missed something).
Results:
Before Variables = 1 Local = 1
After Variables = 2 Local = 1 (result NOT local scoped)
<cffunction name="myFunction" output="true">
<b>Before</b>
Variables = #structCount(variables)#
Local = #structCount(local)#<br />
<cfquery datasource="MyDatasource">
SELECT getDate() AS TestDate
</cfquery>
<b>After</b>
Variables = #structCount(variables)#
Local = #structCount(local)#<br />
</cffunction>
The name attribute is required, but I just tested it & it does not throw an error if the name attribute is missing... how very interesting. Though, without the name attribute - how are you going to get at the result set?
What an interesting question.
UPDATE
Just ran a quick test and did some googling:
<cfquery datasource="#dsn#" result="qresult">
select some_stuff from that_table limit a_bunch
</cfquery>
<cfdump var="#qresult#" />
Dumping the result attribute shows clearly that the query has run and did get a result set - though there appears to be no way to access it. Googling & docs were no help with the defaults or scope.. maybe ping these guys: http://www.bennadel.com/ - I run across a lot of 'experimenting' on their site.
Now the docs do say that the name attribute is required, but I guess I can see situations where it may not be necessary - obviously memory isn't used by the name variable not being there, but what about the result set? so I'm guessing if you run any query where you don't actually need info back from it [anything but a select?] you can get all the info you need by using the result attribute and MAYBE save some memory and execution time?
isn't that a fun thought?
-sean