MSSQL执行:返回Promise和Recordsets

发布于 2025-01-28 08:49:47 字数 1609 浏览 3 评论 0原文

我正在使用Node-MSSQL在事务内执行一堆操作。 Spceweye,按顺序使用的方法为开始tranbulkbulkexeccommits < /代码>。 Here is part of the code, which works as-is:

// bulk insert #columns
.then(result => {  
    const request=new sql.Request(globalTx);
    const tableObj = new sql.Table('#columns');  //#columns
    /* some code to build the desired tableObj*/
    return request.bulk(tableObj);
})

// exec proc
.then(result => { 
    returnObject.lastSuccessfulStep='bulk #data'
    returnObject["#columns rows"]=result.rowsAffected
    const request=new sql.Request(globalTx);
    /* some code to build the desired proc call*/
    return request.execute('spc_sqlDMLCommand');
})

//commit
.then(result => { 
    return globalTx.commit(); // returns promise
})

So you can see the use of chained .thens.最后有一个catch()

我的问题是,在执行方法的情况下通过其回调返回。引用有关第二个参数的参考:

回调(err,记录集,returnValue) - 称为回调 执行完成或发生错误后。 returnValue 也可以作为记录集的属性访问。选修的。如果省略, 返回承诺。

是否有一种方法可以将ERR/RECORKET/RETURNVALUE保存到某些对象,同时仍保持流量,即不必将后续剪切到 s中 s ececute < /code>的回调?

解决方案尝试:

我尝试使用回调:

request.execute('spc_sqlDMLCommand',function cb(a,b,c){
    console.log('a:\n',a,'b:\n',b,'c:\n',c)
});

但是我得到了此错误:

消息:'请求只能在loggedin状态下提出,而不是 sendclientRequest状态',代码:'einvalidstate'

之后,在Internet上添加了此之后,它似乎是关于有问题的多个查询。但是,如果我以上面提到的方式进行操作,它已经对我有用...

I am using node-mssql to perform a bunch of operations inside a transaction. Spcifically, the methods used in order are begin tran,bulk,bulk,exec,commit. Here is part of the code, which works as-is:

// bulk insert #columns
.then(result => {  
    const request=new sql.Request(globalTx);
    const tableObj = new sql.Table('#columns');  //#columns
    /* some code to build the desired tableObj*/
    return request.bulk(tableObj);
})

// exec proc
.then(result => { 
    returnObject.lastSuccessfulStep='bulk #data'
    returnObject["#columns rows"]=result.rowsAffected
    const request=new sql.Request(globalTx);
    /* some code to build the desired proc call*/
    return request.execute('spc_sqlDMLCommand');
})

//commit
.then(result => { 
    return globalTx.commit(); // returns promise
})

So you can see the use of chained .thens. There is a catch() at the end.

My problem is that in the case od the execute method, while the then() /catch() promise structure feels friendly to me, I also need the information that the method returns through its callback. Citing the reference about the second parameter:

callback(err, recordsets, returnValue) - A callback which is called
after execution has completed, or an error has occurred. returnValue
is also accessible as property of recordsets. Optional. If omitted,
returns Promise.

Is there a way to save err/recordsets/returnValue to some objects while still maintaining the flow, ie not having to cut-paste the subsequent then()s into the execute's callback?

Solution attempt:

I tried using a callback:

request.execute('spc_sqlDMLCommand',function cb(a,b,c){
    console.log('a:\n',a,'b:\n',b,'c:\n',c)
});

but I got this error:

message: 'Requests can only be made in the LoggedIn state, not the
SentClientRequest state', code: 'EINVALIDSTATE'

Following this up on the internet, it seems to be about multiple queries having problems. But, it already works for me if I do it in the way mentioned above...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文