0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.