查询中的ORDER BY给了我ORA-00933:SQL命令未正确结束

时间:2017-11-02 18:21:58

标签: oracledb node-oracle oracledb-npm

您好我有一个查询,我使用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,一切正常。请指教。

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

你的意思是ORDER BY NAME des C