coldfusioncoldfusion-11qoq

Query of Queries variable scoping


Is it possible to scope variables when using query of queries? This works

return queryExecute("
        SELECT Title, Slug, Menu, MenuOrder
        FROM qryResult
        WHERE Menu = 'Y'
        ORDER BY MenuOrder
        ",
        [],
        {dbtype = "query"}
        );

This does not work. It throws an error

return queryExecute("
        SELECT Title, Slug, Menu, MenuOrder
        FROM local.qryResult
        WHERE Menu = 'Y'
        ORDER BY MenuOrder
        ",
        [],
        {dbtype = "query"}
        );

Message
Query Of Queries syntax error.
Encountered "local. StackTrace java.sql.SQLException:
Query Of Queries syntax error.
Encountered "local. at coldfusion.sql.imq.jdbcStatement.parseSQL(jdbcStatement.java:590) at coldfusion.sql.imq.jdbcStatement.fetchResult(jdbcStatement.java:547) at ORDER BY MenuOrder ", [], {dbtype = "query"} );


Solution

  • LOCAL is a reserved word in ColdFusion Query of Query and SQL.

    So, wrap LOCAL with square brackets[] like this:

    return queryExecute("
            SELECT Title, Slug, Menu, MenuOrder
            FROM [local].qryResult
            WHERE Menu = 'Y'
            ORDER BY MenuOrder
            ",
            [],
            {dbtype = "query"}
            );
    

    OR

    Use some other name for function local scope i.e.,

    var newLocal = structNew();
    newLocal.qryResult = queryNew("");
    return queryExecute("
            SELECT Title, Slug, Menu, MenuOrder
            FROM newLocal.qryResult
            WHERE Menu = 'Y'
            ORDER BY MenuOrder
            ",
            [],
            {dbtype = "query"}
            );