SQL statement not working when placed in ColdFusion CFC
-
29-06-2021 - |
Domanda
I have the following table join that runs fine in Microsoft SQL Server and returns the expected results.
SELECT d.id1, c.content_type
FROM Document2 AS d INNER JOIN
Content2 AS c ON d.content_id = c.content_id
WHERE (d.class_id = 1)
However when I place the statement into a ColdFusion CFC, the statement will not execute and I am not getting anything to return. Does the syntax change within the CFC file? Is the Microsoft SQL syntax different from the ColdFusion CFC syntax? Or am I missing something else here?
This is the relevant function. I can get this code to work if I use a simple SQL statement that is not a table join. However, when I insert the table join statement nothing will return.
remote array function getcontent() {
var q = new com.adobe.coldfusion.query();
q.addParam( name="searchParam", value="#searchName#" );
q.setDatasource("Document");
q.setSQL("SELECT d.id1, c.content_type FROM Document2
AS d INNER JOIN
Content2 AS c ON d.content_id = c.content_id WHERE (d.class_id = 1)");
var data = q.execute().getResult();
var result = [];
for(var i=1; i<= data.recordCount; i++) {
arrayAppend(result, {"id"=data.d.id1[i], "Type"=data.c.content_type[i]});
}
return result;
}
Soluzione
The problem is this line of code:
arrayAppend(result, {"id"=data.d.id1[i], "Type"=data.c.content_type[i]});
You don't refer to database variables like that - the table alias is not part of the column alias.
You should simply use:
data.id1[i]
If you did actually have a .
in the column alias, then you would need to refer to it using bracket notation, like this:
data['d.id1'][i]
But again, the table alias isn't part of the column alias, so that's not needed.