sqlnode.jssql-serverdatabase-connectionnode-mssql

UnhandledPromiseRejectionWarning: RequestError: Incorrect syntax near '17'


Using mssql(6.3.1) and node(14.15.1).

Query works when I use the template literal as shown in the documentation.

This was my query within template literal and it works. (DB names aren't variables here)

await sql.query`
    INSERT
      INTO
      DB.DB.notes (updated_date,
      updated_user,
      note_description,
      work_order_id,
      created_date ,
      created_user)
    values(
    ${normalizedDate},
    ${username} ,
    ${note_description},
    ${wo_id},
    ${normalizedDate} ,
    ${username} )
`;

But when I try to replace the DB with variables let's say ${DB}, it is throwing error. First it was saying

RequestError: Incorrect syntax near "."

and after that I got

RequestError: Incorrect syntax near "17" (it started from 14 and it's incremented to 17)

This is the documentation, and I have tried a ConnectionPool class, .query('/**query**/'), but none worked for me.

Please suggest what is wrong here.


Solution

  • The syntax you're using in your query generates a parameterised query.

    It is not possible to paramaterise "identifiers" in queries. See Can I parameterize the table name in a prepared statement?