Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL statement not working when placed in ColdFusion CFC

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;
}
like image 474
stat8 Avatar asked Mar 10 '26 02:03

stat8


1 Answers

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.

like image 79
Peter Boughton Avatar answered Mar 11 '26 15:03

Peter Boughton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!