I have to read a table named dbo.Index
from SQL Server in my node js express app but it fails and returns nothing.
Here's my code:
app.get('/getData', (req, res, next) => {
const config = {
user: 'sa',
password: '12345',
server: 'localhost',
database: 'myDB'
}
const pool = new sql.ConnectionPool(config)
pool.connect(err => {
if (err) console.log(err)
console.log('connected.')
const request = new sql.Request(pool)
request.query(`SELECT * FROM dbo.Index`, (error, recordSet) => {
if (err) console.log(error)
res.send(recordSet)
})
})
})
I've tested this code and it works well with other tables but with this specific name dbo.Index
, it fails.
It's necessary for me to read it and I can't change the table name (have no permission).
I use node-mssql package in order to connect to the database.
INDEX
is a Reserved word, so you will need to wrap it in square parentheses, like so:
SELECT * FROM [dbo].[Index]
It's generally best to try and avoid using reserved words for table or column names as it easily leads to confusion, mistakes and bugs.