首页 > 解决方案 > 节点 mssql 在一个 BEGIN TRANSACTION 中进行多个查询

问题描述

想通过我的 API 使用mssql事务进行多次更新。

例子:

  1. 运输表
  2. 列表表
  3. User_Notes 表
  4. 客户_登录表
  5. Push_Notification 表

哪种方法是正确的?

起初我想用原始查询来做。

BEGIN TRANSACTION

CREATE IN SHIPPING

UPDATE IN LISTING

CREATE IN USER_NOTES

UPDATE IN CUSTOMER_LOGIN

CREATE IN PUSH_NOTIFICATION

COMMIT

但是要避免编写像这样的大原始查询。

我也可以将mssql 事务和查询与 (request.query) 一起使用。

const transaction = new sql.Transaction(/* [pool] */)
transaction.begin(err => {
    // ... error checks

    const request = new sql.Request(transaction)
    request.query('create in shipping table', (err, result) => {
        // ... error checks

        transaction.commit(err => {
            // ... error checks

            console.log("Transaction committed.")
        })
    })

    request.query('Update in Listing Table', (err, result) => {
        // ... error checks

        transaction.commit(err => {
            // ... error checks

            console.log("Transaction committed.")
        })
    })

    and so on...
    .
    .
    .
})

标签: node.jssql-servernode-mssql

解决方案


推荐阅读