简体   繁体   中英

Nodejs mssql/msnodesqlv8 issue sending semicolon in database request

Attempting to build a basic API to interact with a MSSQL v12 database using Nodejs. I have been able to connect to the database using the mssql/msnodesqlv8 package but parameterized queries are failing with the following.

I used SQL Server Profiler and saw that the query was coming in as such

and failing. After some investigation it seems to be an issue with the semicolons after the declare and set statements as it is not allowed in TSQL (very new to MSSql, will need to read up). Removing the semicolons did indeed fix the issue when I ran the query manually.

So my question is this.. is there a way to get msnodesqlv8 to work with my version on |Mssql and if yes, how so? Is there a way to omit these semicolons.

If you think there is a better way, i would like to hear it as I am new to Nodejs + MSSql.

Contents of getSecurity.sql

exec database.getSecurityBySecurityId @SecurityKey

contents of index.js

"use strict";

const utils = require("../utils");

const api = async ({ sql, getConnection }) => {
    const sqlQueries = await utils.loadSqlQueries("events");
    const getSecurity = async SecurityKey => {
        const cnx = await getConnection();
        const request = await cnx.request();
        request.input('SecurityKey', SecurityKey);
        return request.query(sqlQueries.getSecurity);
    };

    return {
        getSecurity
    };
};

module.exports = { api };

I was able to work around this by editing the library.

In ./lib/msnodesqlv8.js you can find where is concatenates the query string

... }

    if (input.length) command = `declare ${input.join(',')} ${sets.join(';')};${command};`
    if (output.length) {
      command += `select ${output.join(',')};`
      handleOutput = true
    }

....

Editing this will allow you to control the flow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM