I have structure where I want to replace currentRow
key with cfquery column recID
. This column is integer that is auto incremented in sql
table. For some reason my code is failing to create structure with unique key. Here is my code:
<cfquery name="qryAccounts" datasource="myDB">
SELECT RecID, FirstName, LastName
FROM Accounts WITH (NOLOCK)
</cfquery>
<cfloop query="qryAccounts">
<cfset fnAccounts[RecID] = StructNew()>
<cfset fnAccounts[RecID].RecordID = RecID>
<cfset fnAccounts[RecID].FirstName = FirstName>
<cfset fnAccounts[RecID].LastName = LastName>
</cfloop>
Code above produced this result:
[
{
"FIRSTNAME": "Mike",
"LASTNAME": "Richards",
"RECORDID": 1
},
null,
null,
null,
{
"FIRSTNAME": "John",
"LASTNAME": "Matt",
"RECORDID": 6
}
]
Then I tried to do this:
<cfquery name="qryAccounts" datasource="myDB">
SELECT RecID, FirstName, LastName
FROM Accounts WITH (NOLOCK)
</cfquery>
<cfloop query="qryAccounts">
<cfset fnAccounts["ID"&RecID] = StructNew()>
<cfset fnAccounts["ID"&RecID].RecordID = RecID>
<cfset fnAccounts["ID"&RecID].FirstName = FirstName>
<cfset fnAccounts["ID"&RecID].LastName = LastName>
</cfloop>
And code above produced correct output:
{
"ID1": {
"FIRSTNAME": "Mike",
"LASTNAME": "Richards",
"RECORDID": 1
},
"ID6": {
"FIRSTNAME": "John",
"LASTNAME": "Matt",
"RECORDID": 6
}
}
I'm wondering why first code is failing to produce correct output? Why second version with appended string works fine? Is there any way to fix or work around this problem?