Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass in and out parameters to a mysql stored procedure and return the stored procedure result in the nodejs code

connection.query("call vts_active_tagid('"+RFIDNumber+"','"+Latitude+"','"+Longitude+"','"+datetime+"','"+imeno+"',@passengers,@trip)");

this stored procedure gives one output which contains firstname,phone number and passengerid,how to get those values in the nodejs code

like image 391
user3748717 Avatar asked Dec 07 '22 01:12

user3748717


2 Answers

  1. First thing first, set your mysql to accept multiple statement

    set mysql.createConnection({multipleStatements: true});

  2. Do your query

    query_str = "CALL sp_whatever(?,?,?,@output); select @output"; con.query(query_str, [param1, param2, param3], function(err,rows){ if(err) throw err; console.log(rows); });

Output:

[OkPacket {
    fieldCount: 0,
    affectedRows: 0,
    insertId: 0,
    serverStatus: 10,
    warningCount: 0,
    message: '',
    protocol41: true,
    changedRows: 0 },
  [ RowDataPacket { '@output': -2 } ] ]
like image 58
function1983 Avatar answered Feb 07 '23 17:02

function1983


a sample for you

dbConnection.getConnection(function(err, connection){
        var edituserSQL =  "CALL spEditTheme(?,?,?,?)";
        var resultt;
        connection.query(edituserSQL, [ object.selfid,object.theme_background,object.theme_foreground,dateTimeNow ], function(ERROR,RESULT) {
                if (ERROR) {
                    cb(ERROR, null);
                } else {
                    console.log("exEditUserTheme result");
                    console.log(RESULT);
                    var account = RESULT[0][0].result;
                    cb(null, account);
                }
            });

            connection.release();
    });
like image 34
pitu Avatar answered Feb 07 '23 15:02

pitu