0

嗨,我有一个查询,我正在使用 resultSet 来处理数据。查询工作正常,但是当我附加“ORDER BY name des”时,它给了我 ORA-00933:SQL 命令未正确结束错误。

这是我的查询。

router.post('/report/', jsonParser, function (req, res) {
    var data = req.body,
        startRow = data.startRow,
        numRows = data.numRows,
        sortCol = data.sortCol,
        sortDir = data.sortDir;

    var countQuery = 'SELECT COUNT(*) ' +
        'FROM this_view ' ;

    var query = 'SELECT t.NAME "NAME", ' +
                't.AGE "AGE" ' +
                'FROM this_view t ' +
                'ORDER BY NAME desc';

    var seg,
        orderBy,
        offset;

    orderBy = ' ORDER BY UPPER(' + sortCol + ') ' + sortDir;
    offset = ' OFFSET ' + startRow + ' ROWS FETCH NEXT ' + numRows + ' ROWS ONLY';

    query += orderBy;
    query += offset;

        async.parallel({
        rows: function (callback) {
            pool.getConnection(function (err, connection) {
                logger.info("Begin Connection: " + (new Date().toString()));
                if (err) {
                    logger.error(err.message);
                    return;
                }

                logger.info("Begin execute: " + (new Date().toString()));

                connection.execute(
                    query,
                    [],
                    {
                        resultSet: true,
                        prefetchRows: 1000
                    },
                    function (err, result) {
                        logger.info("End execute: " + (new Date().toString()));
                        // var rowsProcessed = 0;
                        // var startTime;
                        if (err) {
                            logger.error(err.message);
                            callback("Something broke in the first thing");
                            doRelease(connection);
                            return;
                        }
                        var procJson = [];
                        function fetchRowsFromRS(connection, resultSet, numRows) {
                            resultSet.getRows(
                                numRows,  // get this many rows
                                function (err, rows) {
                                    if (err) {
                                        console.error(err);
                                        doClose(connection, resultSet); // always close the result set
                                    } else if (rows.length > 0) {

                                        /**
                                         *  For each row in the result, pushes a new object to the rows array
                                         *  In each new object, the key is assigned and the result row value set
                                         */
                                        for (var i = 0; i < rows.length; i++) {
                                            procJson.push({});
                                            for (var j = 0; j < resultSet.metaData.length; j++) {
                                                procJson[i][resultSet.metaData[j].name.toLowerCase()] = rows[i][j];
                                            }
                                        }

                                        //TODO: Add null handling
                                        logger.info("Send JSON: " + (new Date().toString()));
                                        logger.info("JSON Sent: " + (new Date().toString()));
                                        if (rows.length === numRows) // might be more rows
                                            fetchRowsFromRS(connection, resultSet, numRows);
                                        else
                                            doClose(connection, resultSet); // always close the result set
                                    } else {
                                        callback(null, procJson);
                                        doClose(connection, resultSet); // always close the result set
                                    }
                                });
                        }
                        fetchRowsFromRS(connection, result.resultSet, numRows)
                    });
            });
        },
        totalRows: function (callback) {
            pool.getConnection(function (err, connection) {
                logger.info("Begin Connection: " + (new Date().toString()));
                if (err) {
                    logger.error(err.message);
                    return;
                }

                logger.info("Begin execute: " + (new Date().toString()));

                connection.execute(
                    countQuery,
                    function (err, result) {
                        logger.info("End execute: " + (new Date().toString()));
                        if (err) {
                            logger.error(err.message);
                            callback("Something broke");
                            doRelease(connection);
                            return;
                        }

                        logger.info("Send JSON: " + (new Date().toString()));
                        callback(null, result.rows[0][0]);
                        logger.info("JSON Sent: " + (new Date().toString()));

                        doRelease(connection);
                    });
            });
        }
    }, function(err, result){
        if(err){
            logger.error(err);
        }

        res.send(result);
    });
});

由于我正在分页,我不能在查询本身中使用 ORDER BY 吗?

如果我不在查询中使用 ORDER BY,一切正常。请指教。

4

2 回答 2

1

你有:

var query = 'SELECT t.NAME "NAME", ' +
                't.AGE "AGE" ' +
                'FROM this_view t ' +
                'ORDER BY NAME desc';

然后你做:

orderBy = ' ORDER BY UPPER(' + sortCol + ') ' + sortDir;
offset = ' OFFSET ' + startRow + ' ROWS FETCH NEXT ' + numRows + ' ROWS ONLY';

query += orderBy;
query += offset;

这会给你查询:

SELECT t.NAME "NAME",
       t.AGE "AGE"
FROM   this_view t
ORDER BY NAME desc
ORDER BY UPPER( <sortColumn> ) <sortDir>
OFFSET <startRow> ROWS FETCH NEXT <numRows> ROWS ONLY

您将有两个ORDER BY无效语法的子句。

相反,更改:

orderBy = ' ORDER BY UPPER(' + sortCol + ') ' + sortDir;

orderBy = ', UPPER(' + sortCol + ') ' + sortDir;
于 2020-10-15T09:16:31.390 回答
0

你不是说 ORDER BY NAME des C吗?

于 2017-11-02T18:23:10.587 回答