Question

I want to select currentrow as part of my query - I know I can loop over queries and get the currentrow variable, but I'm doing a QoQ before I use the rows and I want to keep the original rows, e.g.

//Original query
1, Audi
2, BMW
3, Skoda

//QoQ
1, Audi
3, Skoda

This is the code I've got:

q = new Query( datasource = application.db.comcar );
q.setSQL('
    SELECT make, @rownum := @rownum +1 AS  `rownumber` 
    FROM cars, ( SELECT @rownum :=0 )
    LIMIT 10
');
r = q.execute().getResult();

But it's throwing the following error:

Parameter '=' not found in the list of parameters specified

SQL: SELECT make, @rownum := @rownum + 1 AS `rownumber` FROM cars, ( SELECT @rownum    :=0 ) LIMIT 10

This will work in cfquery but I'd like to use it in CFScript. Is there an alternative to using := or some way of escaping this in the query.

Was it helpful?

Solution

It looks like this is a bug in Coldfusion. I could change my code to use cfquery but I'd rather not mix script and tags in my page.

So my workaround was is as follows:

/*
 * based on the existing query 'tmpFields'
 */

// build array of row numbers
arrRowNumbers = [];
cntRowNumbers = tmpFields.recordCount;

for( r = 1; r <= cntRowNumbers; r++ ) {
    arrayAppend( arrRowNumbers, r );
}

// add a new column with the new row number array
queryAddColumn( tmpFields, "fieldNumber", "integer", arrRowNumbers );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top