mysql
  • node.js
  • 2015-10-15 35 views 3 likes 
    3

    我有一个存储过程在MySQL中正常工作。我想用NODEJS来调用它。我如何获得我在nodejs中存储的proc的值?NODEJS - mysqlPool.getConnection - 在MYSQL中调用存储过程 - 返回值

    mysqlPool.getConnection(function(error,conn){ 
         conn.query(" SET @p1 = '" + P_MEMBER_ID   + "' ; " + 
          " SET @p2  =  '" + P_MEMBER_ID_TEST  + "' ; " + 
          " SET @p3  =  @insertResults'" + "' ; " + 
          "CALL PROC_MEMBER_INSERT(" + 
           " @p1 , " + 
           " @p2 , " + 
           " @p3)", function(error,results) 
          { 
           if (error) 
           { 
            console.error(error); 
            throw error; 
           }else{ 
            console.log(results); 
            res.status(200).jsonp({insertResults:test[2][0]});  
           }; 
          }); 
          conn.release(); 
         }); 
    
    
    
    
    
         MYSQL stored proc example. 
         .. 
        CREATE DEFINER=`phil`@`%` PROCEDURE `PROC_MEMBER_INSERT`(
         in P_MEMBER_ID   INT, 
         in P_MEMBER_ID_TEST INT, 
         out RESULT_MESSAGE   varchar(120) 
        ) 
         BEGIN 
         ..... 
    

    感谢 菲尔

    ========================

    我仍然有问题...

    var connection = mysql.createConnection({ 
         host: '11.11.11.11', 
         user: 'phil', 
         password: 'password', 
         port: 3306, 
         database: 'testdb', 
         connectionLimit : 100, 
         multipleStatements: true 
        }); 
    
    
    
        var query = connection.query(" SET @p1  =  '" + P_MEMBER_ID     + "' ; " + 
               " SET @p2  =  '" + P_MEMBER_ID_TEST    + "' ; " + 
               " SET @p3  =   @insertResults "    + " ; " + 
         "CALL PROC_MEMBER_INSERT(" + 
         " @p1 , " + 
         " @p2 , " + 
         " @p3); select @insertResults;"); 
        query 
         .on('error', function(err) { 
          // Handle error, an 'end' event will be emitted after this as well 
          console.log('error ============= ', err); 
         }) 
         .on('fields', function(fields) { 
          // the field packets for the rows to follow 
    
          console.log('fields ============= ', fields); 
    
          //res.status(200).jsonp({insertResults:fields});  
         }) 
         .on('result', function(row) { 
          // Pausing the connnection is useful if your processing involves I/O 
          // connection.pause(); 
    
          console.log('results ============= ', row); 
    
          //res.status(200).jsonp({insertResults:row});  
    
          //processRow(row, function() { 
          // connection.resume(); 
          //}); 
         }) 
         .on('end', function() { 
          // all rows have been received 
         }); 
    
    
        MYSQL stored proc example. 
        .. 
    CREATE DEFINER=`phil`@`%` PROCEDURE `PROC_MEMBER_INSERT`(
        in P_MEMBER_ID   INT, 
        in P_MEMBER_ID_TEST INT, 
        out RESULT_MESSAGE   varchar(120) 
    ) 
        BEGIN 
        .... 
    

    输出看起来是空的。

    没有记录输入。

    ========= output ============== 
    
          api: 2 -- end PROC_MEMBER_INSERT 
          api: 2 P_MEMBER_ID ----------> 5 
          api: 2 P_MEMBER_ID_TEST -----> 1195 
          results ============= { fieldCount: 0, 
           affectedRows: 0, 
           insertId: 0, 
           serverStatus: 10, 
           warningCount: 0, 
           message: '', 
           protocol41: true, 
           changedRows: 0 } 
          results ============= { fieldCount: 0, 
           affectedRows: 0, 
           insertId: 0, 
           serverStatus: 10, 
           warningCount: 0, 
           message: '', 
           protocol41: true, 
           changedRows: 0 } 
          results ============= { fieldCount: 0, 
           affectedRows: 0, 
           insertId: 0, 
           serverStatus: 10, 
           warningCount: 0, 
           message: '', 
           protocol41: true, 
           changedRows: 0 } 
          results ============= { fieldCount: 0, 
           affectedRows: 1, 
           insertId: 0, 
           serverStatus: 10, 
           warningCount: 0, 
           message: '', 
           protocol41: true, 
           changedRows: 0 } 
          fields ============= [ { catalog: 'def', 
           db: '', 
           table: '', 
           orgTable: '', 
           name: '@RESULT_MESSAGE', 
           orgName: '', 
           charsetNr: 63, 
           length: 16777216, 
           type: 251, 
           flags: 128, 
           decimals: 31, 
           default: undefined, 
           zeroFill: false, 
           protocol41: true } ] 
          results ============= { '@insertResults': null } 
    

    =============

    我增加了更多的测试的东西,现在看到它下降到。对('结果,并始终为空。


      results ============= { fieldCount: 0, 
          affectedRows: 0, 
          insertId: 0, 
          serverStatus: 10, 
          warningCount: 0, 
          message: '', 
          protocol41: true, 
          changedRows: 0 } 
          row user not found 
          results ============= { fieldCount: 0, 
          affectedRows: 0, 
          insertId: 0, 
          serverStatus: 10, 
          warningCount: 0, 
          message: '', 
          protocol41: true, 
          changedRows: 0 } 
          row user not found 
          results ============= { fieldCount: 0, 
          affectedRows: 0, 
          insertId: 0, 
          serverStatus: 10, 
          warningCount: 0, 
          message: '', 
          protocol41: true, 
          changedRows: 0 } 
          row user not found 
          results ============= { fieldCount: 0, 
          affectedRows: 1, 
          insertId: 0, 
          serverStatus: 2, 
          warningCount: 0, 
          message: '', 
          protocol41: true, 
          changedRows: 0 } 
    
    
         .on('error', function(err) { 
          // Handle error, an 'end' event will be emitted after this as well 
          console.log('error ============= ', err); 
          return; 
         }) 
         .on('fields', function(fields) { 
          // the field packets for the rows to follow 
          console.log('fields ============= ', fields); 
    
    
    
          if (fields && fields.length) 
           console.log('fields user found :)'); 
          else 
           console.log('fields user not found'); 
    
    
    
    
          //res.status(200).jsonp({winkResults:fields}); 
         }) 
         .on('result', function(row) { 
          // Pausing the connnection is useful if your processing involves I/O 
          // connection.pause(); 
    
          console.log('results ============= ', row); 
    
          if (row && row.length) 
           console.log('row user found :)'); 
          else 
           console.log('row user not found'); 
    
    
          //res.status(200).jsonp({winkResults:row});  
    
          //processRow(row, function() { 
          // connection.resume(); 
          //}); 
         }) 
         .on('end', function() { 
    
    
          // all rows have been received 
         }); 
    

    回答

    0

    修正了从双简单的选择我的问题。

    选择'Inserted'作为双重结果;

    选择“双因失败”结果;

    我没有使用或弄清楚如何获取输出参数。下次我会在这个问题上花更多的时间。

    谢谢 菲尔

    0

    在con.query声明加入select @p3;,刚过CALL PROC_MEMBER_INSERT

    希望这有助于!

    0

    您应该创建一个启用多语句的连接;

    var connection = mysql.createConnection({multipleStatements: true});

    当执行该查询的超时值可以通过字段和结果进行访问,如以下; var query = connection.query(' SET @p1 = '" + P_MEMBER_ID + "' ; " + " SET @p2 = '" + P_MEMBER_ID_TEST + "' ; " + " SET @p3 = @insertResults'" + "' ; " + "CALL PROC_MEMBER_INSERT(" + " @p1 , " + " @p2 , " + " @p3 ');)"; query .on('fields', function(fields, index) { // the fields for the result rows that follow }) .on('result', function(row, index) { // index refers to the statement this result belongs to (starts at 0) }); 字段阵列应该保持您的过程值。

    我无法测试它,但如果输出参数没有在领域存在阵列,然后 - 如果您的逻辑是符合的有但─,您可以添加其他SELECT语句将过程调用,它访问输出如下。

    var query = connection.query(' SET @p1 = '" + P_MEMBER_ID + "' ; " + " SET @p2 = '" + P_MEMBER_ID_TEST + "' ; " + " SET @p3 = @insertResults'" + "' ; " + "CALL PROC_MEMBER_INSERT(" + " @p1 , " + " @p2 , " + " @p3 '); " + "SELECT @p3;")"; query .on('fields', function(fields, index) { // the fields for the result rows that follow }) .on('result', function(row, index) { // index refers to the statement this result belongs to (starts at 0) }); console.log('\n Output ' + row[#index_of_last_select]);

    参考文献: https://github.com/felixge/node-mysql#multiple-statement-queries http://dev.mysql.com/doc/refman/5.5/en/stored-routines-syntax.html

    +0

    感谢您的回复。我仍然有问题。 – philipfwilson

    +0

    我添加了一些更多的测试内容。 – philipfwilson

    相关问题