获取node.js中存储过程的结果

时间:2017-09-30 19:29:43

标签: javascript mysql node.js

我可以在mysql中的一个存储过程中从不同的表(多行)中获取多个选择并在nodejs中检索这些结果吗?

与使用SQL Server的.NET一样,我们可以使用" sqlnextresult"

IMAGE FROM STORE PROCEDURE enter image description here

1 个答案:

答案 0 :(得分:0)

这里有一个使用mssql节点包的示例,您可以找到here有关此包的文档。

var sql = require('mssql'),
    connectionObj = {
        user: 'myUser',
        password: 'myPassword',
        server: 'http://mysqlserver.whatever'
        options: {
            database: 'myDB'
        }
    };

/**
 * Opens connection to sql server.
 *
 * @return {Object} Connection object.
 */
function openConnection() {
    var connection = new sql.Connection(connectionObj);
    return connection;
};

/**
 * Closes connection.
 *
 * @param {Object} connection - Connection Object.
 * @return {Promise} Promise.
 */
function closeConnection(connection) {
    return connection.close();
};

/**
 * Does a request to sql server.
 *
 * @param {Object} connection - Connection object.
 * @param {string} query - Query string to compute.
 * @return {Promise} Promise.
 */
function doRequest(connection, query) {
    return connection.request().query(query);
};

/**
 * Gets Request.
 *
 * @param {Object} connection - Connection object.
 */
function getRequest(connection) {
    return new sql.Request(connection);
};

var request, conn = openConnection();

// First open the connection with DB (method uses the object created at the begining.
conn.connect()
    .then(function() {
        // Now creates a Request.
        request = getRequest(conn);
        // Executes your stored procedure.
        return request.execute('usp_get_Masters');
    })
    .then(function(result) {
        console.log(result); // Here in result you have the result of the stored procedure execution.
    })
    .catch(function(error) {
        console.log(error);
    });