Because structure variables and query variables are references to objects, the rules in the following sections apply to both types of data.
Multiple references to an object
When multiple variables refer to a structure or query object, the object continues to exist as long as at least one reference to the object exists. The following example shows how this works:
<cfscript> depts = structnew();</cfscript> <cfset newStructure=depts> <cfset depts.John="Sales"> <cfset depts=0> <cfoutput> #newStructure.John#<br> #depts# </cfoutput>
This example displays the following output:
Sales
0
After the <cfset depts=0> tag executes, the depts variable does not refer to a structure; it is a simple variable with the value 0. However, the variable newStructure still refers to the original structure object.
You can give a query or structure a different scope by assigning it to a new variable in the other scope. For example, the following line creates a server variable, Server.SScopeQuery, using the local myquery variable:
<cfset Server.SScopeQuery = myquery>
To clear the server scope query variable, reassign the query object, as follows:
<cfset Server.SScopeQuery = 0>
This deletes the reference to the object from the server scope, but does not remove any other references that might exist.
Copying and duplicating objects
You can use the Duplicate function to make a true copy of a structure or query object. Changes to the copy do not affect the original.
When you are not inside a cfloop, cfoutput, or cfmail tag that has a query attribute, you can treat a query column as an array. However, query column references do not always behave as you might expect. This section explains the behavior of references to query columns using the results of the following cfquery tag in its examples:
<cfquery dataSource="cfdocexamples" name="myQuery"> SELECT FirstName, LastName FROM Employee </cfquery>
To reference elements in a query column, use the row number as an array index. For example, both of the following lines display the word "ben":
<cfoutput> #myQuery.Firstname[1]# </cfoutput><br> <cfoutput> #myQuery["Firstname"][1]# </cfoutput><br>
ColdFusion behavior is less straightforward, however, when you use the query column references myQuery.Firstname and myQuery["Firstname"] without using an array index. The two reference formats produce different results.
If you refer to myQuery.Firstname, ColdFusion automatically converts it to the first row in the column. For example, the following lines print the word "ben":
<cfset myCol = myQuery.Firstname > <cfoutput>#mycol#</cfoutput>
But the following lines display an error message:
<cfset myCol = myQuery.Firstname > <cfoutput>#mycol[1]#</cfoutput><br>
If you refer to Query["Firstname"], ColdFusion does not automatically convert it to the first row of the column. For example, the following line results in an error message indicating that ColdFusion cannot convert a complex type to a simple value:
<cfoutput> #myQuery['Firstname']# </cfoutput><br>
Similarly, the following lines print the name "marjorie", the value of the second row in the column:
<cfset myCol = myQuery["Firstname"]> <cfoutput>#mycol[2]#</cfoutput><br>
However, when you make an assignment that requires a simple value, ColdFusion automatically converts the query column to the value of the first row. For example, the following lines display the name "ben" twice:
<cfoutput> #myQuery.Firstname# </cfoutput><br> <cfset myVar= myQuery['Firstname']> <cfoutput> #myVar# </cfoutput><br>