Skip to content

node js -- mysql (Store procedure) #422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
pranavs80 opened this issue Mar 22, 2013 · 8 comments
Closed

node js -- mysql (Store procedure) #422

pranavs80 opened this issue Mar 22, 2013 · 8 comments

Comments

@pranavs80
Copy link

i am having an store procedure in mysql -- which is like this

BEGIN
SELECT 1 AS res;

end

my node js code for this is as 👍

connection.query("call adx.usp_TestNodeJS()",
function(err, results, fields) {
console.log('The solution is: ', results[0].res);
}
);


i am not able to to read the data.
instead of store procedure, i use inline query. then it works fine.

please help me out.

thanks
[pranav sheth]

@kai-koch
Copy link
Collaborator

#407 #385 #391

@felixge
Copy link
Collaborator

felixge commented Mar 22, 2013

@kai-koch thanks for all the help you've provided in the other tickets!

Closing this now.

@felixge felixge closed this as completed Mar 22, 2013
@pranavs80
Copy link
Author

thanks for the quick reply.

but i am not getting how to read the reply of my store procedure.

as it is returning some rows and columns.

@kai-koch
Copy link
Collaborator

http://dev.mysql.com/doc/refman/5.5/en/call.html

if you need a procedure for your stuff, you need to make more than on SQL-Statement.
like in the example in the mysql documentation:

mysql> SET @increment = 10;
mysql> CALL p(@Version, @increment);
mysql> SELECT @Version, @increment;
+--------------+------------+
| @Version | @increment |
+--------------+------------+
| 5.5.3-m3-log | 11 |
+--------------+------------+

If you can rewrite it as a stored function you only need to make one SELECT-Statement to get the returned value.

@pranavs80
Copy link
Author

Thank you very much. now i am getting the output of store procedure.

C:\nginx\html>node spdbconn.js
The solution is: 2
The solution is 1: [ { usercode: -11, res: '
{ usercode: -10, res: 'Casale Media' },
{ usercode: -9, res: 'mansi' },
{ usercode: -8, res: 'Yellow Hammer Network
{ usercode: -7, res: 'Quadrant One' } ]

this output is in the command window.

how should i read this values in code?

@kai-koch
Copy link
Collaborator

function query_callback(err, result) {
    var i,
        ilen;
    if (err) {
        throw err;
    }
    i = 0;
    ilen = result.length;
    while (i < ilen) {
        // Do something with the result:
        console.log ('Row ' + i + ': usercode = "' + result[i].usercode
            + '" res = "' + result[i].res + '"');
        i += 1;
    }
}

[edit: fixed typos and missing counter increment]

@pranavs80
Copy link
Author

i am getting result.lenth as 2 instead of 5.
and in result[0] i got that whole above output.

@kai-koch
Copy link
Collaborator

I do not get your problem, then.
You get back a resultset of nested Arrays and Objects.
So simply access them, as Array or Object.

if this is in result[0][2]:

result[0][2] = [
    { usercode: -11, res: ''},
    { usercode: -10, res: 'Casale Media' },
    { usercode: -9, res: 'mansi' },
    { usercode: -8, res: 'Yellow Hammer Network'},
    { usercode: -7, res: 'Quadrant One' }
];

simply use result[0][2] in a while or for loop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants