tediousjs / node-mssql

Microsoft SQL Server client for Node.js
https://tediousjs.github.io/node-mssql
MIT License
2.23k stars 468 forks source link

Invalid usage of the option NEXT in the FETCH statement #1419

Closed LucasCtrl closed 2 years ago

LucasCtrl commented 2 years ago

Expected behaviour:

Sort persons by their first name, ignore the 5 first and get next 5

Actual behaviour:

Error:

ERROR  Invalid usage of the option NEXT in the FETCH statement.             11:46:56

  at handleError (node_modules\mssql\lib\tedious\request.js:384:15)
  at Connection.emit (node:events:390:28)
  at Connection.emit (node_modules\tedious\lib\connection.js:1048:18)
  at RequestTokenHandler.onErrorMessage (node_modules\tedious\lib\token\handler.js:365:21)
  at Readable.<anonymous> (node_modules\tedious\lib\token\token-stream-parser.js:26:33)
  at Readable.emit (node:events:390:28)
  at addChunk (node:internal/streams/readable:315:12)
  at readableAddChunk (node:internal/streams/readable:289:9)
  at Readable.push (node:internal/streams/readable:228:10)
  at next (node:internal/streams/from:98:31)

Code:

const bodyParser = require('body-parser')
const app = require('express')()
const sql = require('mssql')
const sqlConfig = {
  user: '...',
  password: '...',
  database: '...',
  server: '...',
  pool: {
    max: 10,
    min: 0,
    idleTimeoutMillis: 30000,
  },
  options: {
    encrypt: false,
    trustServerCertificate: true, // change to true for local dev / self-signed certs
  },
}

app.use(bodyParser.json())
app.get('/getJSON', async (req, res) => {
  console.log('req', req.query.test)
  const query = `
SELECT id, firstName
FROM persons
ORDER BY firstName
OFFSET 5 ROWS
FETCH NEXT 5 ROWS ONLY
  `
  console.log(query)
  try {
    // make sure that any items are correctly URL encoded in the connection string
    await sql.connect(sqlConfig)
    const result = await sql.query(query)
    res.json({ result })
  } catch (err) {
    console.error(err)
    res.send('error ' + err)
  }
})

If I run the query in SQL Management Studio, the query run smoothly with no issue.

Software versions

dhensby commented 2 years ago

I'm afraid I don't provide support for debugging SQL query syntax. I recommend looking on stack overflow or similar.

https://www.google.com/search?q=Invalid%20usage%20of%20the%20option%20NEXT%20in%20the%20FETCH%20statement