简体   繁体   English

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

[英]Obtain the result of stored procedure in node.js

Can I get multiple select from distinct tables (many rows) in just one store procedure in mysql and retrieve those result in nodejs?我可以在 mysql 的一个存储过程中从不同的表(多行)中获得多个选择并在 nodejs 中检索这些结果吗?

Like in .NET with SQL Server we can use use "sqlnextresult"就像在 .NET 和 SQL Server 中一样,我们可以使用“sqlnextresult”

IMAGE FROM STORE PROCEDURE来自商店程序的图像在此处输入图片说明

Here you have an example using mssql node package, you can find here the documentation about this package.这里有一个使用mssql节点包的示例,您可以在此处找到有关此包的文档。

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

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

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