繁体   English   中英

Nodejs mssql / msnodesqlv8问题在数据库请求中发送分号

[英]Nodejs mssql/msnodesqlv8 issue sending semicolon in database request

尝试使用Nodejs构建基本API以与MSSQL v12数据库进行交互。 我已经能够使用mssql / msnodesqlv8包连接到数据库,但参数化查询失败,具有以下内容。

代码:'EREQUEST',数字:102,状态:undefined,originalError:{错误:[Microsoft] [SQL Server Native Client 11.0] [SQL Server]''附近的语法不正确。 sqlstate:'42000',代码:102},name:'RequestError'}调试:内部,实现,错误

我使用SQL Server Profiler并看到查询是这样进行的

exec sp_executesql N'declare @SecurityKey nvarchar(MAX); set @ SecurityKey = @ P1; exec database.getSecurityBySecurityId @SecurityKey;',N'@ P1 nvarchar(20)',N'XXXXXXXX'

并失败。 经过一些调查之后,在declare和set语句之后的分号似乎是个问题,因为它在TSQL中是不允许的(MSSql很新,需要读取)。 当我手动运行查询时,删除分号确实解决了问题。

所以我的问题是这个..有没有办法让msnodesqlv8在我的版本上使用| Mssql,如果是的话,怎么回事? 有没有办法省略这些分号。

如果您认为有更好的方法,我想听听它,因为我是Nodejs + MSSql的新手。

getSecurity.sql的内容

exec database.getSecurityBySecurityId @SecurityKey

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 };

我能够通过编辑库来解决这个问题。

在./lib/msnodesqlv8.js中,您可以找到连接查询字符串的位置

...}

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

....

编辑此项将允许您控制流程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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